简体   繁体   English

循环创建新的数据帧

[英]Create new data frames in a loop

I got an original dataframe df with three columns: 我得到了包含三列的原始数据框df

"Person","start","end", where start and end are in "%d.%m.%Y %H:%M:%S" format. “人”,“开始”,“结束”,其中开始结束采用“%d。%m。%Y%H:%M:%S”格式。

head(df)

1 Mark 06.08.2019 00:02:56 06.08.2019 00:14:43
5 Mark 06.08.2019 00:16:31 06.08.2019 00:20:53
6 Vince 06.08.2019 00:18:28 06.08.2019 00:24:30
7   Kate 06.08.2019 00:20:26 06.08.2019 00:23:29
8   Mark 06.08.2019 00:26:34 06.08.2019 00:32:41
9 Vince 06.08.2019 00:31:13 06.08.2019 00:33:14

Person has unique values: 人具有独特的价值观:

people<-unique(df$Person)

people=[Mark,Kate, Vince]

So my goal is to make 3 separated dataframes for those people. 所以我的目标是为那些人制作3个分离的数据框。 Im trying this: 我正在尝试这个:

for (a in people){
  assign(paste("df.", a, sep = ""), subset(a,a[start],a[end]))
}

since start and end are atomic vectors 因为开始结束是原子向量

(is.atomic(...))

but I receive an error saying: 但我收到一条错误消息:

Error in subset.default(a,a[start],a[end]) : 
  object 'start' not found

I wonder how to have separated df s for every person with the same columns as in original DF dataframe? 我想知道如何将每个人的df与原始DF数据帧中的列分开?

You can split your original dataframe into a list of dataframes: 您可以将原始数据框拆分为数据框列表:

df_split <- split(df,f= df$Person)

You can then access each dataframe by using for example df[["Mark"]] . 然后,您可以使用df[["Mark"]]访问每个数据框。 Does that work for you? 那对你有用吗?

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

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