在R中的多个数据帧上的数据操作 [英] Data manipulation on multiple dataframes in R

查看:99
本文介绍了在R中的多个数据帧上的数据操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些数据框A_January,A_February,A_December等,每个列都有10列...



他们都有相同的10列..
我需要对10列之一进行一些数据操作,并在每个数据帧中生成一堆新的列。我可以手动为所有数据帧执行此操作,但是我有400个这样的数据框。



我该怎么做?请让我知道...
假设,我需要在多个数据框上执行相同的操作集合(创建新变量,排序等)
A_January $ New_var< -A_January $ Var1 + A_January $ Var2



我该怎么做?我怎么能把它放在一个循环中,让它发生? PLease让我知道

解决方案

第一步非常重要:不要为每个data.frame创建一个变量。相反,将它们全部放入data.frames列表中:

 数据<  - 列表(A_January,A_February,A_December)

这可能看起来很麻烦,键入,特别是如果你有数百个data.frames。所以如果你能告诉我们你如何创建这些data.frames,我们可能会帮助解决这个问题的根源。



一旦你有一个列表,这很容易修改所有这些:

  data<  -  lapply(data,transform,new_var = Var1 + Var2)


Suppose i have some dataframes A_January, A_February, A_December etc with some 10 columns each...

All of them have the same 10 columns.. I need to do some data manipulation on one of the 10 columns and produce a new bunch of columns in each of the data frames.. I can do this manually for all dataframes, but i have 400 such dataframes..

How do i do this?. please let me know... Suppose, i need to do the same set of operations on multiple dataframes...(create new variables, sort them etc etc) A_January$New_var<-A_January$Var1+A_January$Var2

How do i do this?. How can i put this in a loop and make it happen? PLease let me know

解决方案

First step is very important: do not create a variable for each data.frame. Instead, put them all into a list of data.frames:

data <- list(A_January, A_February, A_December)

This might look cumbersome to type, especially if you have hundreds of data.frames. So if you can tell us how you came to create these data.frames we might help fix the problem at the root.

Once you have a list, it is very easy to modify all of them:

data <- lapply(data, transform, New_var = Var1 + Var2)

这篇关于在R中的多个数据帧上的数据操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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