R 从函数生成数据帧长度(和列数据)
我想生成一个随机长度的数据帧R 从函数生成数据帧长度(和列数据),r,R,我想生成一个随机长度的数据帧 > head(df) "id" "age" 53 12 # randomly chosen data from fn1(){} and fn2(){} 146 31 # 343 22 # ...#randomly generated length from sample(50:5000,1) 问题是我尝试的方式只是一遍又一遍地重复相同的元素: # This just repeats t
> head(df)
"id" "age"
53 12 # randomly chosen data from fn1(){} and fn2(){}
146 31 #
343 22 #
...#randomly generated length from sample(50:5000,1)
问题是我尝试的方式只是一遍又一遍地重复相同的元素:
# This just repeats the same value instead of generating function over and over
a <- fn1(){}
rep(a,15)
[1] "S" "S" "S" "S" "S" "S" "S" ...
#这只是重复相同的值,而不是反复生成函数
a也许像这样的事情会有帮助:
min_rownum <- 10
max_rownum <- 50
num_of_rows <- sample(seq(min_rownum, max_rownum), 1)
min_age <- 1
max_age <- 50
age <- sample(seq(min_age, max_age), num_of_rows, replace = TRUE)
min_ID <- 50
max_ID <- 500
id <- sample(seq(min_ID, max_ID), num_of_rows)
df1 <- data.frame(id, age)
使用此函数,可以使用创建data.frame
my_random_df <- make_random_df()
#> head(my_random_df)
# id age
#1 461 7
#2 86 44
#3 319 8
#4 363 45
#5 59 3
#6 258 49
my_random_df头(my_random_df)
#身份证年龄
#1 461 7
#2 86 44
#3 319 8
#4 363 45
#5 59 3
#6 258 49
也许像这样的东西会有所帮助:
min_rownum <- 10
max_rownum <- 50
num_of_rows <- sample(seq(min_rownum, max_rownum), 1)
min_age <- 1
max_age <- 50
age <- sample(seq(min_age, max_age), num_of_rows, replace = TRUE)
min_ID <- 50
max_ID <- 500
id <- sample(seq(min_ID, max_ID), num_of_rows)
df1 <- data.frame(id, age)
使用此函数,可以使用创建data.frame
my_random_df <- make_random_df()
#> head(my_random_df)
# id age
#1 461 7
#2 86 44
#3 319 8
#4 363 45
#5 59 3
#6 258 49
my_random_df头(my_random_df)
#身份证年龄
#1 461 7
#2 86 44
#3 319 8
#4 363 45
#5 59 3
#6 258 49
这里有一个函数,它从给定向量(vec
)生成给定长度(len
)的数据样本:
createData这里有一个函数,它从给定向量(vec
)生成给定长度(len
)的数据样本:
createDatacreateData
的伸缩范围应与示例中的fn()
s返回的向量类似c
和seq
。你能给我们展示一下你的fn()
s吗?createData
应该可以缩放,只要你的fn()
s返回一个向量,比如示例中的c
和seq
。你能给我们看一个你的fn()
s吗?cbind(col1=replicate(10,RandomStatusColor(),simplify=“vector”),col2=..是我在cbind之后的样子(col1=replicate(10,RandomStatusColor(),simplify=“vector”),col2=..是我在追求的样子
createData <- function(vec, len) {
sample(vec, len, replace = TRUE)
}
nobs <- 20
df <- data.frame(id = createData(vec = c("a", "b", "c"), len = nobs),
age = createData(vec = seq(10, 50, 10), len = nobs))
df