I'm having a prblem with combining similar rows in a DF.
I'm trying to combine the data in a similar c_code column. * I have different types of data im my DF (dates, NA, numbers ETC..)
thank you so much.
I'm having a prblem with combining similar rows in a DF.
I'm trying to combine the data in a similar c_code column. * I have different types of data im my DF (dates, NA, numbers ETC..)
thank you so much.
Here is a data.table approach. This assumes that your data has NA
where the blank spaces are.
df <- structure(list(Time3_Q2 = c(2, 1, NA, NA, NA, 2), Time3_Q1 = c(1,
1, NA, NA, NA, 1), Time2_Q3 = c(NA, 4, 3, 3, NA, NA), Time2_Q2 = c(NA,
3, 2, 2, NA, NA), Time2_Q1 = c(NA, 2, 1, 1, NA, NA), Time1_Q3 = c(3,
NA, NA, NA, 1, 1), Time1_Q2 = c(2, NA, NA, NA, 2, 2), Time1_Q1 = c(1,
NA, NA, NA, 3, 3), c_code = structure(c(1L, 2L, 3L, 1L, 2L, 3L
), .Label = c("AA1234", "AB1234", "AC1234"), class = "factor")), class = "data.frame", row.names = c(NA,-6L))
library(data.table)
setDT(df)
df[,lapply(.SD,function(x){x[!is.na(x)]}),by="c_code"]
If you have empty character vectors ""
where the blanks are, you could use this:
df[,lapply(.SD,function(x){x[x!=""]}),by="c_code"]