2
votes

I am trying to get some means and SD for the 3 groups that I have under the column 'Group' (0, 1 2) in the columns 'Variable1' and 'Variable2'.

DATASET Sorry that I need to provide it like this, but I don't know how to paste it otherwise

> 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")

CODE

I have transformed the variables 1 and 2 in numeric:

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

and asked for means and SDs as:

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)

OUTPUT

The table that I get:

  1. For Variable1:

enter image description here

  1. For Variable2:

    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

In previous analysis I was having an error saying: NAs induced by coercion.

PROBLEM Well, the problem is obvious: Why do I get NAs when my data has numbers on it?

I was able to do the calculations in Excel without a problem. But I am worried that my data actually contains problems. And I need help with this.

Million thanks in advance for your help!

Lili

2

2 Answers

2
votes

Move the na.rm=T inside the parentheses:

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

And the dplyr alternative (note that plyr is officially retired):

library(dplyr)
data %>%
  group_by(Group) %>%
  summarise_if(
    is.numeric,
    list(mean = mean, sd = sd),
    na.rm = TRUE
  )
0
votes

As AHart said the problem was solved with:

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))

The following problems came from the following chunk. I had to # the Produce table to be able to make the whole code to work. Awkward. But this was the solution.

    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"))