查看: 1785| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[统计--软件] R 函数形参的问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
如果想把某个 data frame (比如 trees) 作为形参传入定义的函数里,然后在函数里引用 trees$Girth,怎么样才能做到呢?
. 1point 3acres
还是说函数的形参只能是纯的 string 或者是数字?

求问!

上一篇:关于统计对大数据的贡献
下一篇:Rice M.Stat 和 MA 区别
🔗
midfirefly 2014-9-5 06:43:31 | 只看该作者
全局:
不懂什么叫形参

> a = data.frame(tree=1:10, whatever=rnorm(10)).--
> myfun = function(df) length(df$tree)
> myfun(a)
回复

使用道具 举报

🔗
 楼主| ecb9d16a 2014-9-5 10:01:33 | 只看该作者
全局:
本帖最后由 y5yeyey 于 2014-9-4 19:36 编辑
小K 发表于 2014-9-4 15:43
不懂什么叫形参
. 1point 3 acres
> a = data.frame(tree=1:10, whatever=rnorm(10))
. 1point3acres
谢谢 K姐 的回复。
. 1point 3acres
我想写的函数是这样的:
sampling <- function(data_df)
nselect <- readline("input sample size: ")
nrow <- nrow(data_df)
mysample <- data[sample(1:nrow, nselect), ]
return(mysample)

是一个 sampling 函数,需要输入 data frame (data_df)然后从中取样,不 refer column name。.
想 passing dataframe 到函数不知道为啥出错...



K姐你给的函数把 tree 写进去了,如果换成另外的名字就不 work 了。
回复

使用道具 举报

🔗
midfirefly 2014-9-5 12:56:29 | 只看该作者
全局:
rand = 'tree'
a[rand]
refer by column works too, your code is sampling by row, not col. Χ
?
回复

使用道具 举报

🔗
 楼主| ecb9d16a 2014-9-5 13:37:47 | 只看该作者
全局:
本帖最后由 y5yeyey 于 2014-9-4 22:40 编辑 . 1point 3 acres
小K 发表于 2014-9-4 21:56
rand = 'tree'. 1point3acres
a[rand]
refer by column works too, your code is sampling by row, not col

问题解决了,忘记加 curly brackets 了。。。. 1point 3 acres
谢谢 K姐。
  1. # df = some data frame
  2. sampling <- function(df){
  3. nselect <- readline("Please input a sample size: ")
  4. nrow <- nrow(df).1point3acres
  5. mysample <- df[sample(1:nrow, nselect), ]
  6. return (mysample)
  7. }
  8. print( sampling(trees) )
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表