按组写入多个csv文件
问题描述:
我想按"Rkey"组将数据框分成多个csv文件.例如,以下示例数据将导致生成3个不同的csv文件,每个唯一的Rkey组分别对应一个:R01,R02和R03.这三个文件中的每一个将仅包含属于其各自组的行.这些文件的名称可以与Rkey组名称匹配.
I would like to divide my dataframe into multiple csv files by group "Rkey". For example, the sample data below would result in 3 different csv files being made, one for each unique Rkey group: R01, R02, and R03. Each of the three files would only include the rows belonging to its respective group. The names of these files can match the Rkey group name.
我应该如何解决?非常感谢!
How should I work this out? many thanks!
Pkey Rkey Var1 Var 2
R01_1 R01 0.0000861 0.0021976
R01_2 R01 0.0157098 0.0415425
R01_3 R01 0.0142236 0.0316527
R01_4 R01 0.0000328 0.3496403
R01_5 R01 0.0122406 0.1739126
R02_1 R02 0.0000856 0.0000915
R02_2 R02 0.0002946 0.0006898
R02_3 R02 0.0209878 0.0209901
R02_4 R02 0.0001359 0.0008970
R02_5 R02 0.0011158 0.0023558
R02_10 R02 0.0015220 0.0019581
R02_11 R02 0.0004664 0.0385724
R02_12 R02 0.0000095 0.3224465
R03_1 R03 0.0008863 0.0056300
R03_2 R03 0.0000021 0.0000185
R03_3 R03 0.0000170 0.0001655
答
考虑by
:
by(df, df$Rkey, FUN=function(i) write.csv(i, paste0(i$Rkey[1], ".csv")))