dput(x)
structure(list(State = structure(c(1L, 1L, 2L, 3L, 2L, 4L, 2L,
5L, 5L, 2L), .Label = c("Illinois", "Texas", "California", "Louisiana",
"Michigan"), class = "factor"), Lat = structure(1:10, .Label = c("41.627",
"41.85", "32.9588", "33.767", "33.0856", "30.4298", "29.7633",
"42.4687", "43.0841", "29.6919"), class = "factor"),
Long = structure(1:10, .Label = c("-88.204",
"-87.65", "-96.9812", "-118.1892", "-96.6115", "-90.8999", "-95.3633",
"-83.5235", "-82.4905", "-95.6512"), class = "factor")), .Names = c("State",
"Lat", "Long"), row.names = c(NA, 10L), class = "data.frame")
我需要有另一列說明總數,這將是每個國家的總數。你如何計算獨特的因素,並將它們插入到相同的數據框R
x$Total<-1
然後
library(data.table
x<-data.table(x)
x<-x[,total:=sum(Total),by=State]
是否有更好的/短/高效的數據幀計數因素的辦法:我可以通過創建另一列總這樣做呢?
你嘗試'X <-x [,總數:= N,按=國]'? (不需要首先初始化Total) –
'tabulate(x $ State)[x $ State]'看起來也是有效的 –