I have a data set where I had multiple rows all corresponding to baseline measures. I want to collapse these rows by record id so that each individual only has one row for baseline measures. This means I have to collapse across all variables, some of which are character variables. How do I do this? This is what I've tried:
df.test %>% group_by(id) %>% filter(time == 0) %>%
summarise_all(., collapse=", ")
an example data frame I'm working with looks like this:
data.frame(id = rep(99, 5), time = c(rep(0, 3), 3, 6), v1 = c("blk", NA, NA, 2, 3), v2 = c(NA, 1, NA, 4, 5), v3 = c(NA, NA, 1, 6, 7))
and I need it to look like this:
data.frame(id = rep(99, 3), time = c(rep(0, 1), 3, 6), v1 = c("blk", 2, 3), v2 = c(1, 4, 5), v3 = c(1, 6, 7))
I don't know if summarise is the right function to use here. Basically a problem I have is the summing of the characters which I think is why summarise doesn't really work. Ideally, all I really want to do is combine the information in all the rows where time = 0 by id so that i have a single row of time = 0 for every unique id.
(sorry I'm, not sure how to make the data.frame command print the data frame?)
Help please!
Edit: Example 2
data.frame(id = c(rep(99,5), 100, 101, 101), time = c(rep(0, 3), 3, 6, 0, 0, 0), character = c(NA, NA, "blk", rep(NA, 5)), binary = c(1, rep(NA, 5), 0, NA), continuous = c(NA, NA, 2.29, rep(NA, 5)))
This is close to what my data looks like. What I can say isn't working are the following:
1)the character variable is lost 2)the 0 is lost in the binary variable (even with != is.na(.) instead of != 0 2a) this was atomic and i changed it to factor and now it seems to work 3) the continous variable, 2.29, is lost 4) what's interesting and not pictured here is whole number values that are not 0 seem to be retained but integers are all gone--is this because integers cant be read if the structure is atomic?
Conclusion: i think i need to change all the values from atomic (they were brought into r that way from SPSS)? I will try this in the meantime.
edit2: the issue was not numeric vs atomic. i turned everything numeric and the integers still do not show up.