Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace values in matrix if they have the same beginning?

Tags:

r

matrix

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?

like image 875
nurma_a Avatar asked Dec 10 '25 06:12

nurma_a


1 Answers

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
like image 118
Rui Barradas Avatar answered Dec 11 '25 22:12

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!