由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - R: how to refer function arguments within function?
相关主题
再一次请教apply function 在R中的应用sas base question
How to do Naive Bayes in R?R xlsReadWrite package
[R] How to apply "apply" here?有人对doomsday argument有研究吗?
问一个关于R 的问题【急】一个基本的R的问题,求助。谢谢!大包子答谢
请教一个R的问题(function)请教call execute
R 问题急求R问题
[合集] how to calculate chisq value in Rpass arguments by reference in R
[合集] 问个R里面diff函数的问题?什么是Error in forecast(A, h = 4, level = 95) : unused argument (level = 95)?
相关话题的讨论汇总
话题: function话题: data话题: species话题: fx话题: arguments
进入Statistics版参与讨论
1 (共1页)
S******y
发帖数: 1123
1
I have a function which takes in two arguments -
1) data : input data frame
2) x: a column name
But within function, it seems that I cannot use data$x to properly
reference an existing column within my data frame.
For example,
> library(MASS)
> length(iris$Species)
[1] 150
> fx<-function(data, x) {
+ print(length(data$x))
+ }
>
#hope to return 150. but none of the following is working right!!!!
> fx(iris,Species)
[1] 0
> fx(iris,'Species')
[1] 0
m********n
发帖数: 11
2
try data[x] where x is a vector of characters
S******y
发帖数: 1123
3
Thanks! Got it -
> fx<-function(data, x) {
+ print(length(data[[which(names(data)==x)]]))
+ }
> fx(iris,'Species')
[1] 150
1 (共1页)
进入Statistics版参与讨论
相关主题
什么是Error in forecast(A, h = 4, level = 95) : unused argument (level = 95)?请教一个R的问题(function)
about R function to do N!R 问题
问统计大侠们一个有趣的数学问题[合集] how to calculate chisq value in R
关于在R中对字符数组进行比较[合集] 问个R里面diff函数的问题?
再一次请教apply function 在R中的应用sas base question
How to do Naive Bayes in R?R xlsReadWrite package
[R] How to apply "apply" here?有人对doomsday argument有研究吗?
问一个关于R 的问题【急】一个基本的R的问题,求助。谢谢!大包子答谢
相关话题的讨论汇总
话题: function话题: data话题: species话题: fx话题: arguments