根据R中的列中的值对数据进行分区

时间:2016-09-19 17:23:02

标签: r split partition

我有一个csv文件,格式如下:

./configure --with-php-config=/Applications/MAMP/bin/php/php5.6.10/bin/php-config

现在,我想在R中以2年为增量对这些数据进行分区。我尝试使用split函数但不确定如何定义2年的增量。

预期输出应如下所示:

rec | year | ing
----|------|-----
 1  | 2002 | a
 1  | 2002 | b
 1  | 2002 | c
 2  | 2002 | e
 .  |   .  | . 
 .  |   .  | . 
 4  | 2017 | a 

基本上,将数据分区2年。

2 个答案:

答案 0 :(得分:2)

将每年除以2,然后将其置于模拟2年期间:

df <- read.table(header=TRUE,sep="|",text="
rec|year|ing
1|2002|a
1|2002|b
1|2002|c
2|2002|e
3|2003|a
4|2004|c
4|2004|e
5|2004|a
5|2004|b
6|2005|a
4|2017|a
4|2003|a
")

split(df,floor(df$year/2))

如果您关心每个子集的名称,请将其调整为:

split(df,floor(df$year/2)-min(df$year)/2)

答案 1 :(得分:1)

您可以与split结合使用cut

split imum seqmin imum,这将是year 2年max数据帧的split(df, cut(df$year, seq(min(df$year), max(df$year), 2), include.lowest=TRUE))

df

假设您的数据框为<input size="50" type="file" id="school_logo" name="school_logo">