gpt4 book ai didi

r - 由强制引起的 NA _ 平均值和 SD _ 百分比

转载 作者:行者123 更新时间:2023-12-04 10:47:39 32 4
gpt4 key购买 nike

我正在尝试为“Variable1”和“Variable2”列中“Group”(0, 1 2) 列下的 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,
6.8, 4.5, 3.7, 6.9, 3.8, 10.1, 3.7, 5.1, 6, 5.3, 7.2, 8.6, 7.6,
7.1, 5.9, 5.1, 8.9, 17.3, 27.7, 6.2, 5.8, 26.3, 7.9, 13.5, 9.7,
11.7, 6.8, 18.4, 27.7, 26.3, 12, 46.5, 4.7, 11.4, 5.4, 3.1, 23.4,
12, 16.6, 27.7, 3.7, 6.1, 6.8, 8.1, 20.4, 14.3, 6, 6.9, 8.4,
8.8, 16.9, 6.1, 7.3, 3.5, 10.8, 16, 5.7, 9.5, 10.7, 8.5, 7.7,
6.9, 8.7, 22.4, 8.2, 16.7, 7.6, 6.1, 7.1, 11.9, 5.3, 5.4, 8.3,
6.8, 12.2, 11.5, 6.9, 5.9, 4.5), Group = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 2L, 2L,
2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 3L,
2L, 3L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 3L, 3L, 2L, 1L, 2L, 2L,
1L, 3L, 3L, 3L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 2L, 3L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
3L, 1L, 3L, 1L, 2L, 3L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 1L, 3L, 3L,
2L, 1L, 1L, 1L, 3L, 3L, 1L, 3L, 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))

并要求提供手段和标准差为:
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)

输出

我得到的表:
  • 对于变量 1:

  • enter image description here
  • 对于变量 2:
    Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
    Calling var(x) on a factor x is defunct.
    Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.In addition: Warning message:In mean.default(Variable2) : argument is not numeric or logical: returning NA

  • 在之前的分析中,我遇到了一个错误: NAs induced by coercion.
    问题
    嗯,问题很明显:当我的数据上有数字时,为什么我会得到 NA?

    我能够毫无问题地在 Excel 中进行计算。但我担心我的数据实际上包含问题。我需要这方面的帮助。

    万提前感谢您的帮助!

    莉莉

    最佳答案

    移动 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

    dplyr替代方案(请注意 plyr 已正式退休):
    library(dplyr)
    data %>%
    group_by(Group) %>%
    summarise_if(
    is.numeric,
    list(mean = mean, sd = sd),
    na.rm = TRUE
    )

    关于r - 由强制引起的 NA _ 平均值和 SD _ 百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629551/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com