从R中的长数据集中的每个ID组中子集最后一行
我有一个142415行965列的纵向数据集。对于数据集中的每个ID,都有多行,每个ID的行数不一定相同 我想获得每个ID的最后一行(数据已经排序),并创建一个仅包含这些ID的数据帧,保留所有剩余的964列数据 当我看前面的问题解决这个问题时,很多建议都使用了从R中的长数据集中的每个ID组中子集最后一行,r,dplyr,grouping,subset,plyr,R,Dplyr,Grouping,Subset,Plyr,我有一个142415行965列的纵向数据集。对于数据集中的每个ID,都有多行,每个ID的行数不一定相同 我想获得每个ID的最后一行(数据已经排序),并创建一个仅包含这些ID的数据帧,保留所有剩余的964列数据 当我看前面的问题解决这个问题时,很多建议都使用了aggregate(),我不能使用它(至少从我所知道的情况来看),因为我有太多的列 我确实尝试了以下方法,但这会使我的计算机出错,因此我想知道是否有比列出一个列表然后从中形成一个数据帧更快的方法: data.list<-by(data.
aggregate()
,我不能使用它(至少从我所知道的情况来看),因为我有太多的列
我确实尝试了以下方法,但这会使我的计算机出错,因此我想知道是否有比列出一个列表然后从中形成一个数据帧更快的方法:
data.list<-by(data.in, data.in$ID, tail, n=1)
data.new<-do.call("rbind", as.list(data.list))
data.list我没有你的数据,但是使用dplyr
你可以:
library(dplyr)
iris %>%
group_by(Species) %>%
slice(n())
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# <dbl> <dbl> <dbl> <dbl> <fct>
#1 5 3.3 1.4 0.2 setosa
#2 5.7 2.8 4.1 1.3 versicolor
#3 5.9 3 5.1 1.8 virginica
我没有您的数据,但使用dplyr
您可以:
library(dplyr)
iris %>%
group_by(Species) %>%
slice(n())
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# <dbl> <dbl> <dbl> <dbl> <fct>
#1 5 3.3 1.4 0.2 setosa
#2 5.7 2.8 4.1 1.3 versicolor
#3 5.9 3 5.1 1.8 virginica
由于您的数据是有序的,所以上面的答案可能更有效。如果数据未排序,此方法也有效
我假定您要使用一个列来选择行(例如日期)
您可以通过为大型数据集编制索引,然后选择id和日期行以及索引,然后使用Summary查找正确的索引并过滤大型数据集来解决此问题。这样你就不必在成百上千的柱子周围摇摆了
library(tidyverse)
#Random dataset, date is coded as numeric for practical purposes.
#var1 and var2 pose as the >900 columns you have
large_dataset <- tibble(id=c(1,1,1,2,2,2,2,3,3,4),
date=c(10,15,20,10,15,20,25,10,15,10),
var1=rpois(10,50),
var2=rpois(10,30))
large_dataset
# A tibble: 10 x 4
id date var1 var2
<dbl> <dbl> <int> <int>
1 1 10 41 39
2 1 15 56 32
3 1 20 41 34
4 2 10 46 35
5 2 15 44 38
6 2 20 50 33
7 2 25 58 34
8 3 10 44 28
9 3 15 49 37
10 4 10 62 38
large_dataset <- mutate(large_dataset,index = c(1:nrow(large_dataset)))
key <- select(large_dataset,id,date,index)
key <- group_by(key,id) %>%
summarise(date=max(date)) %>%
left_join(key,by=c("id","date"))
small_dataset <- filter(large_dataset,index %in% key$index) %>%
select(-index) #Removing the index variable since it has served its purpose
id date var1 var2
<dbl> <dbl> <int> <int>
1 1 20 41 34
2 2 25 58 34
3 3 15 49 37
4 4 10 62 38
库(tidyverse)
#随机数据集,出于实际目的,日期被编码为数字。
#var1和var2以>900列的形式出现
大型数据集以上答案可能效果更好,因为您的数据是有序的。如果数据未排序,此方法也有效
我假定您要使用一个列来选择行(例如日期)
您可以通过为大型数据集编制索引,然后选择id和日期行以及索引,然后使用Summary查找正确的索引并过滤大型数据集来解决此问题。这样你就不必在成百上千的柱子周围摇摆了
library(tidyverse)
#Random dataset, date is coded as numeric for practical purposes.
#var1 and var2 pose as the >900 columns you have
large_dataset <- tibble(id=c(1,1,1,2,2,2,2,3,3,4),
date=c(10,15,20,10,15,20,25,10,15,10),
var1=rpois(10,50),
var2=rpois(10,30))
large_dataset
# A tibble: 10 x 4
id date var1 var2
<dbl> <dbl> <int> <int>
1 1 10 41 39
2 1 15 56 32
3 1 20 41 34
4 2 10 46 35
5 2 15 44 38
6 2 20 50 33
7 2 25 58 34
8 3 10 44 28
9 3 15 49 37
10 4 10 62 38
large_dataset <- mutate(large_dataset,index = c(1:nrow(large_dataset)))
key <- select(large_dataset,id,date,index)
key <- group_by(key,id) %>%
summarise(date=max(date)) %>%
left_join(key,by=c("id","date"))
small_dataset <- filter(large_dataset,index %in% key$index) %>%
select(-index) #Removing the index variable since it has served its purpose
id date var1 var2
<dbl> <dbl> <int> <int>
1 1 20 41 34
2 2 25 58 34
3 3 15 49 37
4 4 10 62 38
库(tidyverse)
#随机数据集,出于实际目的,日期被编码为数字。
#var1和var2以>900列的形式出现
大数据集我们也可以使用过滤器
library(dplyr)
iris %>%
group_by(Species) %>%
filter(row_number()== n())
我们也可以使用filter
library(dplyr)
iris %>%
group_by(Species) %>%
filter(row_number()== n())
这回答了你的问题吗?这回答了你的问题吗?