Minh Ha Minh Ha - 3 months ago 20
R Question

Group by in data.table in R which only keep non NA values from columns

I am new to R.

I want to group by a data.table and keep only non NA values from columns.

My table is look like:

c1 c2 c3 c4
1 A NA NA
1 NA B NA
1 NA NA C
2 A1 NA NA
2 NA B1 NA
2 NA NA C1


I want to have a result:

c1 c2 c3 c4
1 A B C
2 A1 B1 C1


Hope anyone can help!

Answer

Try

library(data.table)
setDT(df1)[, lapply(.SD, na.omit) , by = c1]
#    c1 c2 c3 c4
#1:  1  A  B  C
#2:  2 A1 B1 C1

Or

setDT(df)[, lapply(.SD, function(x) x[!is.na(x)]) , by = c1]