重命名R中另一列的列条目

时间:2022-03-01 10:46:45

Simplified example, sample data:

简化示例,示例数据:

data_f <- data.frame(id=rep(1:10),cat=rep("non",10),nam=paste(rep(1:10),rep(1:10),letters[1:10],sep=""))
data_f[,2] <- c("one","","","two","","tree","","four","","")

data_d <- data.frame(nam1=paste(rep(1:10),rep(1:10),letters[1:10],sep=""))

Desired result:

期望的结果:

      nam1  new.name
1     11 a       one   
2     22 b       one
3     33 c       one
4     44 d       two
5     55 e       two
6     66 f       tree 
7     77 g       tree
8     88 h       four
9     99 i       four
10    1010 j     four

In words: if any entries of column nam matches nam1 from data_d take its category, that's column cat from data_f and place it next to the corresponding value of nam1. Please note that the entries of nam1 and nam are scattered randomly not in order as in example.

换句话说:如果列nam的任何条目与data_d中的nam1匹配,则取其类别,即来自data_f的列cat,并将其放在nam1的相应值旁边。请注意,nam1和nam的条目随机分散,不按示例顺序排列。

I'm failing to find efficient solution to this.

我没有找到有效的解决方案。

1 个解决方案

#1


1  

Here's one possibility:

这是一种可能性:

merge(data_d, 
      transform(data_f[-1], cat = ave(cat, cumsum(cat != ""), FUN = function(x) x[1])), 
      by.x = "nam1", by.y = "nam", sort = FALSE)

The result:

结果:

    nam1  cat
1    11a  one
2    22b  one
3    33c  one
4    44d  two
5    55e  two
6    66f tree
7    77g tree
8    88h four
9    99i four
10 1010j four

#1


1  

Here's one possibility:

这是一种可能性:

merge(data_d, 
      transform(data_f[-1], cat = ave(cat, cumsum(cat != ""), FUN = function(x) x[1])), 
      by.x = "nam1", by.y = "nam", sort = FALSE)

The result:

结果:

    nam1  cat
1    11a  one
2    22b  one
3    33c  one
4    44d  two
5    55e  two
6    66f tree
7    77g tree
8    88h four
9    99i four
10 1010j four