简体   繁体   English

使用列值的新数据帧中的子数据帧

[英]subset data frame in new data frame using column values

I am trying to subset a big data.frame in different data.frames automatically. 我试图自动将一个大的data.frame子集到不同的data.frames中。 I have something like: 我有类似的东西:

Type1 Type2 Type3 Info1 Info2 Info3
A     1     Z     a     a     a    
A     2     Y     b     b     b
B     4     X     c     c     c
A     1     Z     d     d     d

I want to create a different data frame for each Type3>Type2>Type1 我想为每个Type3> Type2> Type1创建一个不同的数据框

1st data frame (A1Z): 第一个数据帧(A1Z):

Type1 Type2 Type3 Info1 Info2 Info3
A     1     Z     a     a     a    
A     1     Z     d     d     d

2nd data frame (A2Y): 第二个数据帧(A2Y):

Type1 Type2 Type3 Info1 Info2 Info3
A     2     Y     b     b     b    

... ...

What is the fastest way to reach that? 最快的方法是什么? Thank you!! 谢谢!!

You can use split to produce a list of data.frames: 您可以使用split生成data.frames列表:

> split(df, do.call(paste0, df[,1:3]))
$A1Z
  Type1 Type2 Type3 Info1 Info2 Info3
1     A     1     Z     a     a     a
4     A     1     Z     d     d     d

$A2Y
  Type1 Type2 Type3 Info1 Info2 Info3
2     A     2     Y     b     b     b

$B4X
  Type1 Type2 Type3 Info1 Info2 Info3
3     B     4     X     c     c     c

It's generally recommended to keep related data structures (like data.frames) in a single object (like a list in this case). 通常建议将相关的数据结构(例如data.frames)保留在单个对象(在这种情况下为列表)中。 If you want to assign those data.frames to the global environment you can investigate ?list2env . 如果要将这些data.frames分配给全局环境,可以调查?list2env


As noted by Frank in the comments, splitting like this might not be necessary (although we can't say for sure without more information). 正如弗兰克(Frank)在评论中所指出的那样,这样的拆分可能是不必要的(尽管没有更多信息我们无法确定)。 With dplyr you could use group_by to group the data by those TypeX columns and then perform some manipulations to each group. 使用dplyr,您可以使用group_by将数据按那些TypeX列分组,然后对每个组执行一些操作。 You would start like this: 您将像这样开始:

library(dplyr)
df %>% 
  group_by(Type1, Type2, Type3) %>% 
#   ... more manipulations...

#Source: local data frame [4 x 6]
#Groups: Type1, Type2, Type3
#
#  Type1 Type2 Type3 Info1 Info2 Info3
#1     A     1     Z     a     a     a
#2     A     2     Y     b     b     b
#3     B     4     X     c     c     c
#4     A     1     Z     d     d     d

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

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