具有数据帧参数的R函数不起作用 [英] R - function which has a data frame parameter doesn't work
问题描述
我做了一个简单的代码,它可以将索引号改为name
$ $ $ $ codehange_name = function(x){
valid_user [match(x,valid_user $ id),'name']
}
并将此函数应用于data.frame。
Data.frame名称为'ga.screen',列名为'dimension1'。
ga.screen [,'dimension1'] = sapply(ga.screen [,'dimension1'],change_name)
$ c
接下来我想让这个代码函数
readable_user_id = function(data,col){
data [,col] = sapply(data [,col],change_name)
}
readable_user_id(ga.screen,'dimension1')
这是完全相同的代码,但后者不起作用!
为什么会发生这种情况?这是一个 sapply
问题,它不起作用?或者这是 data.frame
问题,它不能作为参数传递?
解决方案你的函数应该返回修改后的数据
,尝试
可读的用户ID =函数(data,col){
data [,col] = sapply(data [,col],change_name)
data
}
I make a simple code which can change the index number to name
change_name = function(x) {
valid_user[match(x, valid_user$id),'name']
}
and apply this function to data.frame.
Data.frame name is 'ga.screen', and the column name is 'dimension1'.
ga.screen[, 'dimension1'] =sapply(ga.screen[, 'dimension1'], change_name)
It works well.
And next I want to make this code function which is be able to apply various case.
readable_user_id = function(data, col) {
data[, col] = sapply(data[, col], change_name)
}
readable_user_id(ga.screen, 'dimension1')
This is totally same code but the latter one doesn't work!
Why this happens? Is this a sapply
problem which doesn't work in function? or Is this data.frame
problem which can't be passed as parameter?
解决方案 Your function should return the modified data
, try
readable_user_id = function(data, col) {
data[, col] = sapply(data[, col], change_name)
data
}
这篇关于具有数据帧参数的R函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!