Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.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 胁迫引起的NA uu平均和SD uuu百分比_R_Dplyr_Coercion - Fatal编程技术网

R 胁迫引起的NA uu平均和SD uuu百分比

R 胁迫引起的NA uu平均和SD uuu百分比,r,dplyr,coercion,R,Dplyr,Coercion,我正在尝试为“Variable1”和“Variable2”列中的“Group”(0,12)列下的3个组获取一些平均值和SD 数据集很抱歉,我需要这样提供它,但我不知道如何粘贴它 > dput(test) structure(list(Subject_ID = structure(1:163, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",

我正在尝试为“Variable1”和“Variable2”列中的“Group”(0,12)列下的3个组获取一些平均值和SD

数据集很抱歉,我需要这样提供它,但我不知道如何粘贴它

> dput(test)

structure(list(Subject_ID = structure(1:163, .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", 
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100", 
"101", "102", "103", "104", "105", "106", "107", "108", "109", 
"110", "111", "112", "113", "114", "115", "116", "117", "118", 
"119", "120", "121", "122", "123", "124", "125", "126", "127", 
"128", "129", "130", "131", "132", "133", "134", "135", "136", 
"137", "138", "139", "140", "141", "142", "143", "144", "145", 
"146", "147", "148", "149", "150", "151", "152", "153", "154", 
"155", "156", "157", "158", "159", "160", "161", "162", "163"
), class = "factor"), Variable1 = c(270, 206, 291, 81, 254, 253, 
224, 324, 139, 164, 113, 140, 158, 202, 324, 99, 201, 222, 244, 
197, 91, 200, 366, 203, 151, 202, 237, 194, 72, 227, 134, 270, 
281, 260, 276, 110, 76, 364, 296, 222, 300, 203, 100, 295, 219, 
235, 277, 202, 132, 243, 222, 317, 153, 232, 266, 254, 224, 267, 
83, 302, 230, 217, 474, 235, NA, 244, 287, 261, 261, 222, 218, 
328, 302, 282, 105, 213, 254, 323, 259, 297, 94, 429, 297, 253, 
207, 234, 171, 225, 310, 216, 172, 392, 240, 306, 167, 40, 230, 
405, 278, 140, 385, 207, 96, 301, 130, 137, 260, 178, 229, 174, 
157, 116, 44, 244, 237, 220, 240, 223, 317, 232, 132, 168, 289, 
55, 267, 317, 251, 196, 160, 149, 308, 281, 287, 193, 161, 279, 
270, 273, 397, 138, 272, 208, 231, 291, 245, 351, 238, 100, 352, 
66, 224, 273, 262, 259, 217, 334, 264, 293, 412, 157, 164, 263, 
199), Variable2 = c(NA, 6.1, 4.3, 18.6, 6.4, 6.7, 6.9, 9.2, 4.2, 
6.9, 14.3, 21.6, 2.9, 11.9, 6.1, 12.7, 6, 5.2, 4.5, 6.1, 9.6, 
3.5, 6.6, 5.4, 3.9, 3.1, 4.3, 5.1, 69.1, 4.1, 22.3, 6.3, 8, 10.2, 
6.4, 11.2, 13.8, 6.5, 6.2, 4.8, 6.5, 5.5, 29.9, 10.9, 10.7, 4.7, 
5.3, 25.4, 11.8, 10, 9, 8, 6.8, 6.6, 6, 7.7, 6.5, 6.4, 18.2, 
11.9, 11.8, 3.9, 18.8, 7.6, 5.1, 7.7, 5.3, 5.7, 5.3, 6.3, 2.7, 
4.5, 6.3, 7.5, 11.9, 6.8, 3.2, 12, 7.8, 4, 48, 3.9, 7.9, 15.4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L, 3L, 1L, 3L, 2L, 3L, 1L, 1L, 2L, 
3L), .Label = c("0", "1", "2"), class = "factor")), row.names = c(NA, 
-163L), class = "data.frame")
代码

我已将变量1和2转换为数字:

data$Variable1 <- as.numeric(as.character(data$Variable1))
data$Variable2 <- as.numeric(as.character(data$Variable2))

data$Variable1在括号内移动
na.rm=T

library(plyr)
meanSD<-ddply(data, .(Group),summarise,
              meanV1= mean(Variable1,na.rm = TRUE),
              SD_V1=sd(Variable1,na.rm = TRUE),
              meanV2= mean(Variable2,na.rm = TRUE),
              SD_V2=sd(Variable2,na.rm = TRUE))

> meanSD
  Group   meanV1    SD_V1    meanV2     SD_V2
1     0 207.6964 80.41798  8.650909  5.415816
2     1 224.4815 72.39221 11.327273 10.108128
3     2 255.8077 83.28431 10.161538  9.297399

正如AHart所说,问题是通过以下方式解决的:

library(plyr)
meanSD<-ddply(data, .(Group),summarise,
              meanV1= mean(Variable1,na.rm = TRUE),
              SD_V1=sd(Variable1,na.rm = TRUE),
              meanV2= mean(Variable2,na.rm = TRUE),
              SD_V2=sd(Variable2,na.rm = TRUE))

说得好。我把它改成了dplyr,并添加了一条说明,plyr正式“退休”,希望它能工作。谢谢!我试过这个代码,它对某些方法和SDs有效,但对其他方法无效。给我这个错误:函数(x,na.rm=FALSE,type=7)中的错误:形式参数“na.rm”与多个实际参数相匹配shard表示没有看到更多内容,但尝试将摘要段更改为:
summary\u if(is.numeric,list(mean=mean,sd=sd),na.rm=T)
如果有效,我将更新答案。你的意思是:意思是不完全正确。ddply(作为plyr的一部分)将永久失效,并由dplyr取代。我已使用上述建议更新了响应。如果你之前提到的错误仍然存在,我需要更多信息来尝试其他诊断。
library(dplyr)
data %>%
  group_by(Group) %>%
  summarise_if(
    is.numeric,
    list(mean = mean, sd = sd),
    na.rm = TRUE
  )
library(plyr)
meanSD<-ddply(data, .(Group),summarise,
              meanV1= mean(Variable1,na.rm = TRUE),
              SD_V1=sd(Variable1,na.rm = TRUE),
              meanV2= mean(Variable2,na.rm = TRUE),
              SD_V2=sd(Variable2,na.rm = TRUE))
    new = New_DH[!duplicated(New_DH$names), ]
write.csv(new, file="new.csv")

# Produce table
#columns_to_display = colnames(new)[1:5]
#for_display = new[columns_to_display]
#kable_input = kable(head(for_display), "latex", booktabs=T)
#kable_styling(kable_input, latex_options = c("striped", "scale_down"))