R 根据每个可能值的数量更改数据帧列中的值

R 根据每个可能值的数量更改数据帧列中的值,r,dataframe,R,Dataframe,我有一个如下所示的数据框: chr <- c(1,1,1,1,1) b1 <- c('HP', 'HP', 'CP', 'CP', 'KP') b2 <- c('HP', 'HP', 'CP', 'CP', 'KP') b3 <- c('CP', 'KP', 'CP', 'HP', 'CP') b4 <- c('CP', 'KP', 'CP', 'HP', 'CP') b5 <- c('CP', 'CP', 'KP', 'KP', 'HP') b6 <-

我有一个如下所示的数据框:

chr <- c(1,1,1,1,1)
b1 <- c('HP', 'HP', 'CP', 'CP', 'KP')
b2 <- c('HP', 'HP', 'CP', 'CP', 'KP')
b3 <- c('CP', 'KP', 'CP', 'HP', 'CP')
b4 <- c('CP', 'KP', 'CP', 'HP', 'CP')
b5 <- c('CP', 'CP', 'KP', 'KP', 'HP')
b6 <- c('CP', 'CP', 'KP', 'KP', 'HP')
b7 <- c('CP', 'KP', 'HP', 'CP', 'CP')
b8 <- c('CP', 'KP', 'HP', 'CP', 'CP')
df <- data.frame(chr, b1,b2,b3,b4,b5,b6,b7,b8)
您可以尝试:

t(apply(df[,-1], 1, function(rg){
                occ_rg <- table(rg)
                rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                return(rg)}))
你会得到:

#  chr c1 b1 b2 b3 b4 b5 b6 b7 b8 c2
#1   1  1 CP CP CP CP CP CP CP CP 11
#2   1  2 KP KP KP KP CP CP KP KP 12
#3   1  3 CP CP CP CP KP KP CP CP 13
#4   1  4 CP CP CP CP KP KP CP CP 14
#5   1  5 KP KP CP CP CP CP CP CP 15
编辑2

如果您有“HP”、“CP”和“KP”以外的其他值,并且希望将“HP”替换为“CP”或“KP”,具体取决于哪个值出现最多,您可以执行以下操作:

df[, grepl("^b", colnames(df))] <- t(apply(df[, grepl("^b", colnames(df))], 
                                           1, 
                                           function(rg){
                                                   occ_rg <- table(rg)
                                                   occ_rg <- occ_rg[grepl("KP|CP", names(occ_rg))]
                                                   rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                                                   return(rg)}))

df[,grepl(“^b”,colnames(df))]你能显示预期的结果吗?因此,它应该是这样的:'chr请在你的帖子中更新它。你可以在
apply
调用中输入相关列的编号:而不是
df[,-1]
,将df[,num\u\u相关列]
,例如
df[,c(2:5,8:10)]`如果bxx列是2、3、4、5、8、9、10列,您可以在最后一次调用中输入,而不是
df@Margins,我编辑了我的答案,仅修改“相关”列,因为它们是唯一以“b”开头的列。让我知道它现在是否有效,或者您是否需要其他更改。@嗯,为此,您必须修改答案;-)。这就是为什么最好把所有的“信息”放在你的初始问题中,这样答案就完全满足了你的需要。我会做一个“EDIT2”@Margins,看看我的EDIT2,让我知道它是否按照你想要的方式工作
t(apply(df[,-1], 1, function(rg){
                occ_rg <- table(rg)
                rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                return(rg)}))
df <- data.frame(chr=df[, 1], t(apply(df[,-1], 1, function(rg){
                                                  occ_rg <- table(rg)
                                                  rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                                                  return(rg)})), 
                 stringsAsFactors=F)
#  chr b1 b2 b3 b4 b5 b6 b7 b8
#1   1 CP CP CP CP CP CP CP CP
#2   1 KP KP KP KP CP CP KP KP
#3   1 CP CP CP CP KP KP CP CP
#4   1 CP CP CP CP KP KP CP CP
#5   1 KP KP CP CP CP CP CP CP
df[, grepl("^b", colnames(df))] <- t(apply(df[, grepl("^b", colnames(df))], 
                                           1, 
                                           function(rg){
                                                   occ_rg <- table(rg)
                                                   rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                                                   return(rg)}))
#  chr c1 b1 b2 b3 b4 b5 b6 b7 b8 c2
#1   1  1 HP HP CP CP CP CP CP CP 11
#2   1  2 HP HP KP KP CP CP KP KP 12
#3   1  3 CP CP CP CP KP KP HP HP 13
#4   1  4 CP CP HP HP KP KP CP CP 14
#5   1  5 KP KP CP CP HP HP CP CP 15
#  chr c1 b1 b2 b3 b4 b5 b6 b7 b8 c2
#1   1  1 CP CP CP CP CP CP CP CP 11
#2   1  2 KP KP KP KP CP CP KP KP 12
#3   1  3 CP CP CP CP KP KP CP CP 13
#4   1  4 CP CP CP CP KP KP CP CP 14
#5   1  5 KP KP CP CP CP CP CP CP 15
df[, grepl("^b", colnames(df))] <- t(apply(df[, grepl("^b", colnames(df))], 
                                           1, 
                                           function(rg){
                                                   occ_rg <- table(rg)
                                                   occ_rg <- occ_rg[grepl("KP|CP", names(occ_rg))]
                                                   rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)]
                                                   return(rg)}))
df[, grepl("^b", colnames(df))] <- # only the columns beginning with b are considered (so the other ones will remain untouched)

      t( # the results of apply will be transposed
         apply(df[, grepl("^b", colnames(df))], # apply on df with only the columns beginning by b
               1, # by row
               function(rg){ # a function that takes a vector "rg" as input
                   occ_rg <- table(rg) # computes the table
                   occ_rg <- occ_rg[grepl("KP|CP", names(occ_rg))] # keep only the occurrences of either "KP" or "CP"
                   rg[grep("HP",rg)] <- names(occ_rg)[which.max(occ_rg)] # replace in the vector rg the "HP" elements by "KP" or "CP" depending on which occurs the most
                   return(rg) # finally returns the vector rg
               }))