Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算标准偏差,而不考虑由R中其他两个向量的水平确定的行?_R_Loops_Match_Aggregate_Standard Deviation - Fatal编程技术网

如何计算标准偏差,而不考虑由R中其他两个向量的水平确定的行?

如何计算标准偏差,而不考虑由R中其他两个向量的水平确定的行?,r,loops,match,aggregate,standard-deviation,R,Loops,Match,Aggregate,Standard Deviation,我用总参数计算同一行业和同一地区的公司的标准差 sd_lsales我们可以试试 df1$sd_lsales <- unsplit(lapply(split(df1[-1], df1$Region), function(x) { sd1 <- sapply(unique(x$Sector), function(y) { i1 <- x$Sector!=y sd(x$lsal

我用总参数计算同一行业和同一地区的公司的标准差

sd_lsales我们可以试试

df1$sd_lsales <- unsplit(lapply(split(df1[-1], df1$Region), function(x) {
                  sd1 <- sapply(unique(x$Sector), function(y) {
                  i1 <- x$Sector!=y
                  sd(x$lsales[i1])

                    })
                 sd1[x$Sector]
               }

             ), df1$Region)

df1$sd_lsales你能试试这个吗

library(dplyr)

sd_lsales <- aggregate(lsales, by=list(region, sector), function(x) sqrt((sum((x-mean(x))^2) - (x-mean(x))^2)/n()-1)   )
库(dplyr)

sd_lsales Hardik,很抱歉我的回复太晚了,我在您的代码中使用了dplyr包,答案是:n()中的错误:不应直接调用此函数。不确定是什么原因造成的…@Hardik:现在尝试了长度(x)。产生与akrun不同的NAN和标准偏差。如果您从他的解决方案中复制粘贴df1,并将其与代码的结果进行比较。我再次检查了akrun代码的结果,结果是正确的。
df1 <- structure(list(Region = c("Vienna", "Vienna", "Vienna", "Berlin", 
"Berlin", "Berlin", "Berlin"), Sector = c("Food", "Other Manufacturing", 
 "Food", "Food", "Manufacturing", "Manufacturing", "IT"), lsales = c(363000000L, 
5930000L, 150000000L, 39200000L, 203900000L, 298000000L, 30339200L
 )), .Names = c("Region", "Sector", "lsales"), class = "data.frame", row.names = c("1", 
"2", "3", "505", "506", "507", "508"))
library(dplyr)

sd_lsales <- aggregate(lsales, by=list(region, sector), function(x) sqrt((sum((x-mean(x))^2) - (x-mean(x))^2)/n()-1)   )