将变量名传递给sapply [英] Passing variable name into sapply

查看:87
本文介绍了将变量名传递给sapply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个玩具数据集:

df1 <-data.frame(c("267119002","257051033",NA,"267098003","267099020","267047006"))
names(df1)[1]<-"ID"

df2 <-data.frame(c("257051033","267098003","267119002","267047006","267099020"))
names(df2)[1]<-"ID"
df2$vals <-c(11,22,33,44,55)

和玩具代码:

fetcher <-function(x){
  y <- df2$vals[which(match(df2$ID,x)==TRUE)]
  return(y) 
}

sapply(df1$ID,function(x) fetcher(x))

sapply语句中,而不是使用df1$ID,我需要使用变量名.如:

In the sapply statement, instead of using df1$ID, I need to use a variable name. As in:

col <-"ID"
sapply(df1[col],function(x) fetcher(x))

但是,当我以此方式执行操作时,它不会遍历df1$ID的所有值.这样,它仅对第一个值执行sapply.输出示例:

However when I do it this way it does not iterate through all the values of df1$ID. This way it only does sapply on the first value. Example output:

> sapply(df1[col],function(x) fetcher(x))
ID 
33 
> sapply(df1$ID,function(x) fetcher(x))
[1] 33 11 22 55 44

那为什么会这样呢?我需要使用变量名而不是确切的列名,因为我需要将此变量名应用于每次程序运行时都会有所不同的不同列.但是我需要将它不仅应用于第一行,而且还应用于每一行.

So why is this happening? I need to use the variable name instead of the exact column name as I need to apply this to different columns that will vary each time the program runs. But I need it to apply to each row not just the first row.

推荐答案

区别在于df1[col]返回一列数据帧,而df1$ID返回向量/因数.使用您的代码,您需要一个向量/因数,因此您可以

The difference is that df1[col] returns a one column dataframe and df1$ID returns a vector/factor. Using your code you want a vector/factor, hence you may

使用df1[, col]

sapply(df1[, col],function(x) fetcher(x))

或双括号df1[[col]]

sapply(df1[[col]],function(x) fetcher(x))

.

这篇关于将变量名传递给sapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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