为什么没有sample.default()函数

时间:2016-02-19 18:59:38

标签: r

我有一个过程,我想从矢量中随机选择,偶尔可以是长度为1的整数。

sample(x,1)长度为1时,

x会从向量x返回一个样本。当sample.int(x)长度为1时,它会调用x+1函数,该函数的整数采样小于sample(4,1) >3 sample("4",1) >"4" #I want a simpler way of getting as.numeric(sample(as.character(4),1))) >4 。有没有一种方法可以强制R对单个整数值进行采样,就像整数是一个字符一样?

使事情具体化:

as.numeric(as.character())

此行为在8.2.33的r inferno中注明,它表明了 Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in //check if result has value if let value = Response.result.value { let json = JSON(value) //Do the parsing if !json.isEmpty { if let arrData = json.array { for dict in arrData { dict["Id"].int dict["Title"].string dict["Body"].string dict["InsertDate"].string //store this data into object add the object into array } } } } } kludge。

帮助页面需要一半来解释此行为。除了长期混淆行为的历史之外,还有其他原因吗?

1 个答案:

答案 0 :(得分:2)

推荐的方法是编写一个包装函数来处理单位长度列表

resample <- function(x, ...) x[sample.int(length(x),...)]

请参阅the examples here

> resample(4,1)
[1] 4
相关问题