简体   繁体   English

在R中使用for循环创建数据帧

[英]Creation of data frames using a for loop in r

I'd like to write a loop that creates five new data sets in R, each one containing a different number of observations from an original data frame, df. 我想编写一个在R中创建五个新数据集的循环,每个循环包含与原始数据帧df不同数量的观察值。

Here is my current code, it outputs the value of dfi as a string rather than the actual object ("df[4:42 + i]" instead of df[4:42 + i]). 这是我当前的代码,它以字符串而不是实际对象的形式输出dfi的值(“ df [4:42 + i]”而不是df [4:42 + i])。

for(i in 1:5)
{  nam <- paste("df",i, sep="")
assign(nam, eval(paste("df","[1:44 + ",i,",]", sep="")))
}

I'd like to return the df object when it loops, but I don't know how to do that. 我想在循环时返回df对象,但我不知道该怎么做。 Any suggestions? 有什么建议么? Thank you very much in advance. 提前非常感谢您。

Given a sample dataset: 给定样本数据集:

df <- mtcars

And here's the list of frames: 这是框架列表:

list_of_frames <- lapply(1:5, function(i) df[1:3 + i,])
list_of_frames[[3]]
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

(If you really like the names, you can also do (如果您真的喜欢名字,也可以

names(list_of_frames) <- paste0("df", 1:5)
list_of_frames[["df3"]]

If you really need to keep each variable separate, then here's the loop: 如果您确实需要将每个变量分开,那么这里是循环:

ls() # proof that they don't exist yet
# [1] "df"
for (i in 1:5) assign(paste0("df", i), df[1:3 + i,])
ls()
# [1] "df"  "df1" "df2" "df3" "df4" "df5" "i"  
df3
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

I take this as sampling a data frame n number of times with repeating being okay. 我认为这是对数据帧进行n次采样,重复一次就可以了。 You can do this with lapply and some tidyverse. 您可以使用lapply和一些tidyverse来完成此操作。

floor(runif(5, 10, 30))

This generates 5 integers from 10 to 30. Change these as you like. 这将生成5个从10到30的整数。可以根据需要更改它们。

function(x) mtcars %>% sample_n(x)

This takes a dataframe (mtcars), and samples some number of rows from the dataframe. 这需要一个数据帧(mtcars),并从该数据帧中采样一些行。

lDF <- lapply(floor(runif(5, 10, 30)), function(x) mtcars %>% sample_n(x))

This puts it together using lapply with creates a list of dataframes that you can reference as lDF[1] as you like 结合使用lapply和创建一个数据帧列表,您可以根据需要将其称为lDF [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM