- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为“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")
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)
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.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/
在 R Language Definition 中,对NA值进行了简要描述,其中一部分说 ... In particular, FALSE & NA is FALSE, TRUE | NA is TR
我对 R 还很陌生,目前遇到一个问题,数据如下所示: ID h1 h2 h3 h4 h5 h6 h7 h8
我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N
我想用 dplyr 解决以下问题。最好与窗口功能之一一起使用。我有一个包含房屋和购买价格的数据框。下面是一个例子: houseID year price 1 19
在data.frame(或data.table)中,我想用最接近的先前非NA值“向前填充”NA。一个使用向量(而不是 data.frame)的简单示例如下: > y yy [1] NA NA NA
这是一个示例数据框: > df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4), + NA, NA,
我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE。我想将这些值转换为 NA 但找不到方法。 最佳答案 使用 dfr[dfr==""]=NA哪里dfr是你的数据框。
我有一个示例表,其中包含一些但不是全部 NA需要替换的值。 > dat id message index 1 1 1 2 1 foo 2 3 1
在 R 中,如果从 NA 中减去一个数字,它将返回 NA: > x NA - x [1] NA 但是如果你尝试从 NA 中减去一个日期,它会返回一个错误: > x NA - x Error in
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个答案) 关闭 4 年前。 为什么在 R 中会这样? >
我有一个看起来像这样的数据框: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59
我有一个按“id”分组的数据框和一个包含缺失值的变量“age”,NA。 在每个“id”中,我想替换“age”的缺失值,但只“填充”之前 第一个 非NA 值。 data % group_by(id) %
我有如下所示的数据框: df df id value v1 v2 v3 1 1 351 NA 1 0 2 2 585 0 1 1 3 3 321 NA 0 1 4
所以我有一个数据集,只需查看它,数据集中就有明显的 NA。 > dput(bmi.cig) structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4
我有两个 30m x 30m 的光栅文件,我想从中采样点。在采样之前,我想从图像中移除模糊区域。我求助于 R 和 Hijman 的 Raster 包来完成这项任务。 使用 drawPoly(sp=TR
我有以下时间序列 > y y[c(1,2,5,9,10)] y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 201
这个问题在这里已经有了答案: Replace missing values (NA) with most recent non-NA by group (7 个回答) 5年前关闭。 我有一个 DF 个
我想向我的数据框中添加一个新变量 (N_notNAs),它定义了其他任何变量是否为 NA。 x y z N_notNAs 2 3 NA NA NA 1 3 NA 2
我有一个名为 SMOKE 的因子,级别为“Y”和“N”。缺失值被替换为 NA(从初始级别“NULL”开始)。然而,当我查看这个因素时,我得到这样的结果: head(SMOKE) # N N Y Y
假设我有以下 data.frame: t<-c(1,1,2,4,5,4) u<-c(1,3,4,5,4,2) v<-c(2,3,4,5,NA,2) w<-c(NA,3,4,5,2,3) x<-c(2,
我是一名优秀的程序员,十分优秀!