我有一个数据帧: head(df) Year Find Found6982 1901 267 2466983 1901 271 2516984 1902 317 2366985 1903 339 2446986 1904 339 2606987 1903 345 155255 1902 47 455256 1901 46 NA5257 1906 45 1505258 1905 42 245259 1910 42 785260 1910 41 N
head(df) Year Find Found 6982 1901 267 246 6983 1901 271 251 6984 1902 317 236 6985 1903 339 244 6986 1904 339 260 6987 1903 345 15 5255 1902 47 45 5256 1901 46 NA 5257 1906 45 150 5258 1905 42 24 5259 1910 42 78 5260 1910 41 NA
当我试图聚合它时:
aggdata <-aggregate(df, by=list(Year), FUN=sum, na.rm=TRUE)
我收到一个错误
Error in aggregate.data.frame(AndelKvinnorUttax, by = list(Year), FUN = sum, : object 'Year' not found
我找不到问题……
我的解决方案是:
aggr=cbind(aggregate(data=df,Find~Year, FUN=sum,na.rm=TRUE),aggregate(data=df,Found~Year, FUN=sum,na.rm=TRUE))[,c(1,2,4)]
任何人?
最好的祝福!
aggregate不会自动评估data参数中data.frame范围内的Year.你必须明确告诉它在哪里找到年份,即….aggdata <-aggregate(df, by=list(df$Year), FUN=sum, na.rm=TRUE) # Group.1 Year Find Found #1 1901 5703 584 497 #2 1902 3804 364 281 #3 1903 3806 684 259 #4 1904 1904 339 260 #5 1905 1905 42 24 #6 1906 1906 45 150 #7 1910 3820 83 78