I have a dataframe df with only string values. I need to aggregate these rows on idand session and fill the NA values. My original dataframe has 50 columns but this is just an example dataframe. You can assume that for each combination of id and session the values (string1 or string2) are the same, if they don't have an NA value.
session <- c('s1', 's1', 's1', 's2', 's2', 's2')
string1 <- c('first_string1', NA, 'first_string1', NA, 'first_string3', NA)
string2 <- c(NA, 'second_string2', 'second_string2', 'second_string4', NA, NA)
df <- data.frame(id, session, string1, string2)
df
id session string1 string2
1 a s1 first_string1 <NA>
2 a s1 <NA> second_string2
3 a s1 first_string1 second_string2
4 b s2 <NA> second_string4
5 b s2 first_string3 <NA>
6 b s2 <NA> <NA>
The final dataframe should look like this:
id session string1 string2
1 a s1 first_string1 second_string2
2 b s2 first_string3 second_string4
I have tried to using the aggregate function but I can't figure out how to get this working
first_string3is associated withsecond_string4, given that neither are actually adjacent to each other anywhere in the starting data frame?idandsession