Passing variable name into sapply

浪尽此生 提交于 2019-12-13 04:06:43

问题


Here is a toy dataset:

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)

and toy code:

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

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

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))

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.


回答1:


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

use df1[, col]

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

or double brackets df1[[col]]

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

.



来源:https://stackoverflow.com/questions/21215705/passing-variable-name-into-sapply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!