Exc
Exc

Reputation: 183

Replacing value in one column with one in another based on string match

Let's say I have a dataframe with about 7 columns. The first column contains a list of numbers and the others have a number followed by words.

Suppose the number is say 15. I want R to look at the other 6 columns, find which one has "15" and copy the contents of that entry into my number column.

How do I do this?

EDIT : assume that there wont be multiple matches when R searches.

Upvotes: 0

Views: 87

Answers (1)

bgoldst
bgoldst

Reputation: 35314

If I understand your requirement correctly, you can use grep() with value=T on each row independently, passing a regex which must be dynamically constructed from the number in the first column, and overwrite the number column with the resulting vector.

## generate data
set.seed(1L); NR <- 5L; NC <- 7L;
nums <- sample(seq_len(NR));
df <- data.frame(num=nums,t(sapply(nums,function(num) paste0(sample(if (NC-1L>=num) seq_len(NC-1L) else c(num,seq_len(NC-2L))),sample(letters,NC-1L,T)))));
df;
##   num X1 X2 X3 X4 X5 X6
## 1   2 6e 5r 3j 2u 1m 4s
## 2   5 6d 2g 4k 3a 1j 5w
## 3   4 3r 6u 5c 2s 1k 4v
## 4   3 4m 6t 3s 2m 5w 1l
## 5   1 2k 1x 5h 4l 6i 3q

## solution
df$num <- sapply(seq_len(nrow(df)),function(ri) grep(value=T,paste0('^',df$num[ri],'($|[^0-9])'),unlist(df[ri,-1L])));
##   num X1 X2 X3 X4 X5 X6
## 1  2u 6e 5r 3j 2u 1m 4s
## 2  5w 6d 2g 4k 3a 1j 5w
## 3  4v 3r 6u 5c 2s 1k 4v
## 4  3s 4m 6t 3s 2m 5w 1l
## 5  1x 2k 1x 5h 4l 6i 3q

This assumes that every number matches exactly one column value.

Upvotes: 2

Related Questions