全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
898 2
2016-12-27
我想要的结果:sample(9,1) 返回值:9
而R软件,sample(9,1) 返回值:0-9之间随机。
使用sample(c(9),1) 同样不能只返回:9
sample("a",1)就可以返回唯一值:"a"
求解?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-12-27 23:13:38
?sample里说得很清楚了
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x). See the examples.
某些情况方便,某些情况奇怪。
如果你真的只对变量a抽一次,as.numeric(sample(as.character(a),1))应该可以满足你的要求,如果抽一次以上应该写上replace=T避免报错。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-12-28 03:26:43
这个的确是这个函数的一个限制,
你可以加个判断,如果长度为1,就返回他的值
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群