0

I'm having a prblem with combining similar rows in a DF.

DataFrame I have now

the DataFrame I want

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.

Ido
  • 201
  • 1
  • 8
  • Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check `?dput()`). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Mar 24 '20 at 13:10

1 Answers1

0

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"]
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57