Hard_Course
Hard_Course

Reputation: 187

Custom pattern matching between two columns and replacing to keep within-column groups consistent

I have a dataset like this:

dat <- read.table(text = "
   var seq1 seq2
1   A1    1    4
2   AL    1    4
3    E    1    2
4    F    1    4
5   A3    2    3
6   A6    2    3
7   Si    2    3
8   A5    3    2
9   IN    3    2
10  Z5    3    2  
", header = TRUE)

And I need to do some custom column matching, so that when variables are similarly grouped between columns, they take on the value of the first column. ex A5,IN,Z5 are grouped similarly in both columns so they should all be 3's in the second column. The trick is, I also need the groups to remain consistent within the 2nd column so variable E for instance would also need to change from 2 to 3.

desired output:

dat2 <- read.table(text = "
   var seq1 seq2
1   A1    1    1
2   AL    1    1
3    E    1    3
4    F    1    1
5   A3    2    2
6   A6    2    2
7   Si    2    2
8   A5    3    3
9   IN    3    3
10  Z5    3    3  
", header = TRUE)

I've tried various combos of:

dat <- dat %>%
rowwise%>%
  mutate('seq2' = ifelse(dat$seq==dat$seq2,dat$seq,dat$seq2))

but can't seem to crack it.

Upvotes: 0

Views: 33

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101034

Hope this is the thing you want

transform(
    dat,
    seq2 = as.numeric(ave(seq1, seq2, FUN = \(x) names(which.max(table(x)))))
)

which gives

   var seq1 seq2
1   A1    1    1
2   AL    1    1
3    E    1    3
4    F    1    1
5   A3    2    2
6   A6    2    2
7   Si    2    2
8   A5    3    3
9   IN    3    3
10  Z5    3    3

Upvotes: 0

Related Questions