简体   繁体   English

如何在名称中包含特定字符串的一系列列上应用相同的函数?

[英]How to apply the same function over a series of columns with a specific string in their names?

I have a list of variables named as av1 av2 av3…av144 in my data frame (dat).我的数据框 (dat) 中有一个名为 av1 av2 av3…av144 的变量列表。 I want to recode these into another series of variables say main1 main2 main3… main144 as such:我想将这些重新编码为另一系列变量,比如 main1 main2 main3... main144 如下:

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?谁能告诉我如何在不重写同一行 144 次的情况下将这个 ifelse 命令应用于两组变量? I have unsuccessfully experimented with "grep" trying to extract by column names but I think I was in the wrong direction…我尝试使用“grep”尝试按列名提取未成功,但我认为我走错了方向……

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. SimonO101 提供了一个数据集,比我预期的要简单。 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

Here's a similar approach, with some reproducible data for illustrative purposes.这是一个类似的方法,为了说明目的,有一些可重复的数据。 I find the locations in dat that meet the condition and change those values in a results df to 10.我在 dat 中找到满足条件的位置,并将结果 df 中的这些值更改为 10。

set.seed(1)
dat <- data.frame( av1 = sample(8) , av2 = sample(8) , av3 = sample(8) )
#  av1 av2 av3
#1   3   6   6
#2   8   1   7
#3   4   2   3
#4   5   7   4
#5   1   3   5
#6   7   8   1
#7   2   4   2
#8   6   5   8


#  Initialise a df to hold results, fill with FALSE values (0)
out <- `[<-`(dat , , , 0 )

#  Find where values should be TRUE
ind <- sapply( dat , function(x) x %in% c( 5 , 6 , 8 ) )

#  Change to 10
out[ ind ] <- 10

#  Change names if desired
names(out) <- gsub( "av" , "main" , names(dat) )
#  main1 main2 main3
#1     0    10    10
#2    10     0     0
#3     0     0     0
#4    10     0     0
#5     0     0    10
#6     0    10     0
#7     0     0     0
#8    10    10    10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM