Reputation: 1
I have a data frame named df and I need to combine certain rows together. I made a mistake entering the data and some individuals came out without a treatment. I know below that G13B is actually G13S. My data is organized as soon below and I would like to add very specific rows together.
Individual v1 v2 v3 Treatment
G13B 0 4 6 NA
G13S 1 2 1 Control
G34B 0 4 6 NA
G34S 1 2 1 Control
Individual v1 v2 v3 Treatment
G13S 1 6 7 Control
G34S 1 6 7 Control
I attempted to do Rbind, but all it does it add the column above it.
Upvotes: 0
Views: 312
Reputation: 206232
If you want to collapse rows based on the first three characters of the Individual ID, you could do something with dplyr
. First, your sample data
dd<-structure(list(Individual = structure(1:4, .Label = c("G13B",
"G13S", "G34B", "G34S"), class = "factor"), v1 = c(0L, 1L, 0L,
1L), v2 = c(4L, 2L, 4L, 2L), v3 = c(6L, 1L, 6L, 1L), Treatment = structure(c(NA,
1L, NA, 1L), .Label = "Control", class = "factor")), .Names = c("Individual",
"v1", "v2", "v3", "Treatment"), class = "data.frame", row.names = c(NA,
-4L))
then you can do
library(dplyr)
dd %>% group_by(IND=substr(Individual,1,3)) %>%
summarize(
Individual=max(as.character(Individual), na.rm=T),
v1=sum(v1),
v2=sum(v2),
v3=sum(v3),
Treatment=min(as.character(Treatment), na.rm=T)) %>%
ungroup() %>% select(-IND)
which returns
Individual v1 v2 v3 Treatment
1 G13S 1 6 7 Control
2 G34S 1 6 7 Control
Upvotes: 1