R中的子集数据帧使用循环 [英] subset data frame in R using loop
问题描述
我有一个数据框,如下所示:
----------
索引ID日期金额
2 1001 2010-06-08 0
21 1001 2010-10-08 10
6 1002 2010-08-16 30
5 1002 2010-11-25 20
9 1003 2010-01-01 0
8 1003 2011-03-06 10
12 1004 2012-03-12 10
11 1004 2012 -06-21 10
15 1005 2010-01-01 30
13 1005 2010-04-06 20
我想对这些数据进行子集,以便我有新的数据框架,每个ID都有一个这样的
索引ID日期金额
2 1001 2010-06-08 0
21 1001 2010-10-08 10
和
6 1002 2010-08-16 30
5 1002 2010- 11-25 20
等等。
<我不需要保存新的数据帧,但使用它来执行一些基本的计算。此外,我想在我的整个表上做这个,包含超过10000个ID,因此需要一个循环。我试过这个
temp< - data.frame(Numb = c(),Dt = c(),Amt = c ())
for(i in seq_along(stNew $ ID)){
temp [i,]< - subset(stNew,stNew [i,] == stNew $ ID [i])
}
但没有工作。有什么建议么?谢谢。
看看 list2env 和 split 函数。这里有一些使用虹膜数据集的例子。
两种方式:
list_df< - split(iris,iris $ Species)根据iris的值,将数据集分解成数据集列表$ Species
list2env(list_DF,envir = .GlobalEnv)将列表分成单独的数据集
单向:
code> list2env(split(iris,iris $ Species),envir = .GlobalEnv)
或者您可以为 c $ c循环的为新数据集分配自定义名称:
<$ c $ (i)1(长度(iris_split))中的
$)))))))))))))))))))))))))))))))))))))))))) ){
assign(new_names [i],iris_split [[i]])
}
更新例子
I have a data frame that looks like this:
----------
index ID date Amount
2 1001 2010-06-08 0
21 1001 2010-10-08 10
6 1002 2010-08-16 30
5 1002 2010-11-25 20
9 1003 2010-01-01 0
8 1003 2011-03-06 10
12 1004 2012-03-12 10
11 1004 2012-06-21 10
15 1005 2010-01-01 30
13 1005 2010-04-06 20
I want to subset this data so that i have new data frames, one for each ID like this
index ID date Amount
2 1001 2010-06-08 0
21 1001 2010-10-08 10
and
6 1002 2010-08-16 30
5 1002 2010-11-25 20
and so on.
I dont need to save the new data frames, but use it to perform some basic calculations. Also i want to do this on my entire table consisting of more than 10000 IDs and hence the need for a loop. I tried this
temp <- data.frame(Numb=c(),Dt=c(),Amt=c())
for (i in seq_along(stNew$ID)){
temp[i,] <- subset(stNew, stNew[i,]==stNew$ID[i])
}
but that didnt work. Any suggestions? Thanks.
Take a look at the list2env and split function. Hereby some examples using the iris dataset.
two way:
list_df <- split(iris, iris$Species) #split the dataset into a list of datasets based on the value of iris$Species
list2env(list_DF, envir= .GlobalEnv) #split the list into separate datasets
one way:
list2env(split(iris, iris$Species), envir = .GlobalEnv)
Or you can assign custom names for the new datasets with a for
loop:
iris_split <- split(iris, iris$Species)
new_names <- c("one", "two", "three")
for (i in 1:length(iris_split)) {
assign(new_names[i], iris_split[[i]])
}
updates with examples
这篇关于R中的子集数据帧使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!