我在R中具有以下数据框:
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel files|*.xlsx";
//serialVal is name of a variable, not necessary at all if you don't need a specific file name
saveFileDialog.FileName = serialVal;
if (saveFileDialog.ShowDialog() == true)
{
workbook.SaveAs(saveFileDialog.FileName);
workbook.Dispose();
return;
}
我想通过分区从中获得一组数据帧,分区的规则是:在ID col1
1 10
2 40
3 5
4 33
5 37
6 2
7 102
8 11
9 76
处拆分(并在col1<10
处省略行,尽管可以做到这一点当然,稍后)。所以要求的输出:
df1:
col1<10
df2:
ID col1
1 10
2 40
df3:
ID col1
1 33
2 37
谢谢您的见解。
答案 0 :(得分:1)
这将创建一个列表,其元素为各个数据框:
grp <- cumsum(df$col1 < 10)
by(df, grp, subset, col1 >= 10)
给予:
grp: 0
ID col1
1 1 10
2 2 40
------------------------------------------------------------
grp: 1
ID col1
4 4 33
5 5 37
------------------------------------------------------------
grp: 2
ID col1
7 7 102
8 8 11
9 9 76
可复制形式的输入:
Lines <- "ID col1
1 10
2 40
3 5
4 33
5 37
6 2
7 102
8 11
9 76"
df <- read.table(text = Lines, header = TRUE)