如何为R for循环中的每次迭代创建一个新的数据框? [英] How to create a new dataframe for each iteration in R for loop?

查看:128
本文介绍了如何为R for循环中的每次迭代创建一个新的数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于R中for循环的每次迭代的结果创建一个新的数据帧.我有一个"weekly_df".具有多个列的每周值.我想创建一个新的数据框,其中包含每个列(基于迭代数)中的值以及日期列(Year和Week,它们始终是最后两列).

当前,我有以下数据列(每一行都有数字值)...

A1分数,A1体积,A2分数,A2体积,A3分数,A3体积,年,周

所以本质上,我想为"Year"之前的每一列创建一个新的数据框.

A1分数,年,周

A1量,年,周

如此等等,但是我不确定如何循环我的数据来为每个数据创建一个新的数据框.我目前有以下...

 的(i in 1:(length(names(weekly_df))-2)){print(weekly_df [,c(i,((length(names(weekly_df)))-1),(length(names(weekly_df)))])}} 

这会为我想要的每个数据帧打印正确的小标题",但是我不确定如何在我的环境中将其转换为保存的数据帧以供以后使用.

这是可重现的数据集:

A1分数,A1体积,A2分数,A2体积,A3分数,A3体积,年,周

1、2、3、4、5、6、2016、1

7、8、9、10、11、12、2016、2

13,14,15,15,16,17,18,2016,3

在我的环境中,我想要的输出是作为数据帧的以下各项:

A1分数,年,周

1,2016,1

7,2016,2

13,2016,3

A1量,年,周

2,2016,1

8,2016,2

14,2016,3

A2分数,年,周

3,2016,1

9,2016,2

15,2016,3

...等等,每一行都创建一个数据框.

解决方案

您可以使用:

  n<-ncol(weekly_df)list_df<-lapply(名称(weekly_df)[-c(n-1,n)],函数(x)cbind(weekly_df [x],weekly_df [c(n-1,n)])) 

这将返回一个数据框列表,通常最好将数据保留在列表中,因为它更易于管理并且避免在全局环境中创建大量变量,但是如果您希望将它们作为单独的数据框,则可以命名列表并使用 list2env .

  list_df<-paste0('数据',seq_along(list_df))list2env(list_df,.GlobalEnv) 

I am attempting to create a new dataframe based on the results from each iteration of a for loop in R. I have a "weekly_df" which has weekly values for multiple columns. I would like to create a new dataframe which contains the values from each column (based on the iteration number) along with the date columns (Year and Week, which are always the last two columns).

Currently, I have the following data columns (which have numeric values for each row)...

A1 Score, A1 Volume, A2 Score, A2 Volume, A3 Score, A3 Volume, Year, Week

So essentially I want to create a new dataframe for each column up until 'Year'...

A1 Score, Year, Week

A1 Volume, Year, Week

So on and so fourth, but I am unsure how to loop my data to create a new dataframe for each of these. I currently have the following...

for (i in 1:(length(names(weekly_df)) -2)) {
print(weekly_df[,c(i,((length(names(weekly_df))) -1), (length(names(weekly_df))))]) }

This prints the correct 'tibble' for each of the dataframes that I want but I'm unsure how to transform these into saved dataframes in my environment to be used later on.

Here is a reproducible dataset:

A1 Score, A1 Volume, A2 Score, A2 Volume, A3 Score, A3 Volume, Year, Week

1, 2, 3, 4, 5, 6, 2016, 1

7, 8, 9, 10, 11, 12, 2016, 2

13, 14, 15, 16, 17, 18, 2016, 3

And my desired output is each of the following as a dataframe in my environment:

A1 Score, Year, Week

1, 2016, 1

7, 2016, 2

13, 2016, 3

A1 Volume, Year, Week

2, 2016, 1

8, 2016, 2

14, 2016, 3

A2 Score, Year, Week

3, 2016, 1

9, 2016, 2

15, 2016, 3

... so and so fourth as to create a dataframe from each column.

解决方案

You can use :

n <- ncol(weekly_df)
list_df <- lapply(names(weekly_df)[-c(n-1, n)], function(x) 
               cbind(weekly_df[x], weekly_df[c(n-1, n)]))

This will return you a list of dataframes, usually it is better to keep data in a list since it is easier to manage and avoids creating lot of variables in the global environment but if you want them as separate dataframes, you can name the list and use list2env.

list_df <- paste0('data', seq_along(list_df))
list2env(list_df, .GlobalEnv)

这篇关于如何为R for循环中的每次迭代创建一个新的数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆