I have a big matrix with groceries. Some of the values are identical but have different names. For example:
Ketchup Ketchupwithgarlic Ketchupspicy Chips Chipsorganic
0 1 0 0 1
1 0 0 0 0
0 0 0 1 0
1 0 0 0 0
What I want to do is to combine these two vectors into one vector if one name begins with the exact same name, so the output looks like this:
Ketchup Chips
1 1
1 0
0 1
1 0
what should I do?
I believe this does what you want. At least with the dataset you have provided it does. And it doesn't depend on hard coded column names.
With the data as read by the code in @MKR answer:
nms <- names(df)
inx <- which(sapply(seq_along(nms), function(i) any(grepl(paste0("^", nms[i]), nms[-i]))))
result <- sapply(inx, function(i) rowSums(df[, grep(nms[i], nms)]))
colnames(result) <- nms[inx]
result
# Ketchup Chips
#[1,] 1 1
#[2,] 1 0
#[3,] 0 1
#[4,] 1 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With