如何将相同的函数应用于一系列具有特定字符串名称的列? [英] How to apply the same function over a series of columns with a specific string in their names?

查看:148
本文介绍了如何将相同的函数应用于一系列具有特定字符串名称的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框(dat)中有一个名为av1 av2 av3 ... av144的变量列表。我想将这些重新编码到另一系列变量中,比如main1 main2 main3 ... main144,例如:
$ b $ pre $ code dat $ main1 <-ifelse( dat $ av1 == 5或dat2 $ av1 == 8或dat $ av1 == 6,10,0)
dat $ main2 <-ifelse(dat $ av2 == 5或dat2 $ av2 == 8或dat $ av2 == 6,10,0)
#...
dat $ main144< -ifelse(dat1 $ av144 == 5或dat $ av144 == 8或dat $ av144 == 6,10 ,0)

任何人都可以告诉我如何将这个ifelse命令应用于两组变量重写同一行144次?我试图用列名尝试提取grep的尝试失败,但我认为我的方向是错误的......



非常感谢您提前,

解决方案

现在轻度测试:

  dat [gsub(av,main,names(dat))]<  -  
lapply(dat [grep(av,names(dat))],
function(col){ ifelse(col == 5 | col == 8 | col == 6,10,0)})



< SimonO101提供的数据集并不像我期待的那么复杂。这是一个稍微复杂一些的测试,但仍然是我的代码的最小测试(现在我修复了第一个版本中缺失的逗号)AND(修正了将行分配给列的逻辑错误):
(1 = 2,2 = av1 =样本(8),av2 =样本(8),av3 =样本(b) 8));

dat < - cbind(dat,sapply(dat [grep(av,names(dat))],
function(col){ifelse(col == 5 | col == 8 | col == 6,10,0)}))
dat
#----------------
one two av1 av2 av3 av1 av2 av3
1 1 2 4 3 4 0 0 0
2 1 2 6 2 5 10 0 10
3 1 2 7 7 8 0 0 10
4 1 2 5 8 1 10 10 0
5 1 2 2 5 6 0 10 10
6 1 2 1 1 7 0 0 0
7 1 2 3 4 3 0 0 0
8 1 2 8 6 2 10 10 0
#--------------
names(dat)[6:8]< - gsub(av,主要,名字(dat)[6:8])
dat
#-----------------
one two av1 av2 av3 main1 main2 main3
1 1 2 4 3 4 0 0 0
2 1 2 6 2 5 10 0 10
3 1 2 7 7 8 0 0 10
4 1 2 5 8 1 10 10 0
5 1 2 2 5 6 0 10 10
6 1 2 1 1 7 0 0 0
7 1 2 3 4 3 0 0 0
8 1 2 8 6 2 10 10 0


I have a list of variables named as av1 av2 av3…av144 in my data frame (dat). I want to recode these into another series of variables say main1 main2 main3… main144 as such:

dat$main1<-ifelse (dat$av1==5 or dat2$av1==8 or dat$av1==6,10,0)
dat$main2<-ifelse (dat$av2==5 or dat2$av2==8 or dat$av2==6,10,0)
#…
dat$main144<-ifelse (dat1$av144==5 or dat$av144==8 or dat$av144==6,10,0)

Could anyone please tell me how can I apply this ifelse command over two sets of variables without re-writing the same line 144 times? I have unsuccessfully experimented with "grep" trying to extract by column names but I think I was in the wrong direction…

Thank you very much in advance,

解决方案

Now lightly tested:

dat[gsub("av", "main", names(dat))] <- 
      lapply(dat[grep("av", names(dat))], 
            function(col) { ifelse (col==5 | col==8 | col==6, 10, 0) } )

SimonO101 provided a dataset that was less complex than I was expecting was being discussed. Here is a slightly more complex but still reasonably minimal test of my code (now that I fixed the missing comma that was in the first version) AND (fixed the logical error in assigning rows to columns):

  dat <- data.frame( one=1, two=2, av1 = sample(8) , av2 = sample(8) , av3 = sample(8) ); 

   dat <- cbind(dat,      sapply(dat[grep("av", names(dat))], 
              function(col) { ifelse (col==5 | col==8 | col==6, 10, 0) } ) )
 dat
 #----------------
  one two av1 av2 av3 av1 av2 av3
1   1   2   4   3   4   0   0   0
2   1   2   6   2   5  10   0  10
3   1   2   7   7   8   0   0  10
4   1   2   5   8   1  10  10   0
5   1   2   2   5   6   0  10  10
6   1   2   1   1   7   0   0   0
7   1   2   3   4   3   0   0   0
8   1   2   8   6   2  10  10   0
#--------------
 names( dat)[6:8] <- gsub("av", "main", names(dat)[6:8])
 dat
#-----------------
  one two av1 av2 av3 main1 main2 main3
1   1   2   4   3   4     0     0     0
2   1   2   6   2   5    10     0    10
3   1   2   7   7   8     0     0    10
4   1   2   5   8   1    10    10     0
5   1   2   2   5   6     0    10    10
6   1   2   1   1   7     0     0     0
7   1   2   3   4   3     0     0     0
8   1   2   8   6   2    10    10     0

这篇关于如何将相同的函数应用于一系列具有特定字符串名称的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆