我有一个data.table,并希望按组计算统计数据.
R) set.seed(1)
R) DT=data.table(a=rnorm(100),b=rnorm(100))
Run Code Online (Sandbox Code Playgroud)
这些群体应该由
R) quantile(DT$a,probs=seq(.1,.9,.1))
10% 20% 30% 40% 50% 60% 70% 80% 90%
-1.05265747329 -0.61386923071 -0.37534201964 -0.07670312896 0.11390916079 0.37707993057 0.58121734252 0.77125359976 1.18106507751
Run Code Online (Sandbox Code Playgroud)
我如何计算出每箱的平均值b,比如b=-.5我是否[-0.61386923071,-0.37534201964]在bin中3
在R语言中,我想使用switch语句来替换nest if else语句.我想为新列分配值,我的想法是:
## Create a function to seperate the case
Range <- function(x)
if (CityData_Group_Copy$BadDebtNum[x] < 26)
{ CityData_Group_Copy$BadDebtRange[x] <- "1~25"}
else if(CityData_Group_Copy$BadDebtNum[x] > 25 && CityData_Group_Copy$BadDebtNum[x] < 51)
{CityData_Group_Copy$BadDebtRange[x] <- "26~50"}
else if(CityData_Group_Copy$BadDebtNum[x] > 51 && CityData_Group_Copy$BadDebtNum[x] < 76)
{CityData_Group_Copy$BadDebtRange[x] <- "51~75"}
else if(CityData_Group_Copy$BadDebtNum[x] > 75 && CityData_Group_Copy$BadDebtNum[x] < 101)
{CityData_Group_Copy$BadDebtRange[x] <- "76~100"}
else if(CityData_Group_Copy$BadDebtNum[x] > 100)
{ CityData_Group_Copy$BadDebtRange[x] <- "100+"}
## Assign the result to the new column "CityData_Group_Copy$BadDebtRange"
for(i in 1: nrow(CityData_Group_Copy) ){
Range(i)
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个解决方案: …