索引与$和[[]]之间的区别???

时间:2018-02-22 10:50:21

标签: r indexing dplyr

我有一个关于在R中索引数据帧的问题。这是代码:

Gewicht <- data %>%
    group_by(data[[376]]) %>%
    summarise(weights = mean(data[[10190]], na.rm = TRUE))


Gewicht2 <- data %>%
    group_by(data[[376]]) %>%
    summarise(weights = mean(Weights, na.rm = TRUE))


a <- seq(1:10)
b <- rep(c("male", "female"),5)
c <- seq(1:10)

data <- as.data.frame(cbind(a,b,c))
data$c <- as.numeric(data$c)

newdata <- data %>%
    group_by(data[[2]]) %>%
    summarise(Mean = mean(c, na.rm = TRUE))

newdata2 <- data %>%
    group_by(data[[2]]) %>%
    summarise(Mean = mean(data[[3]], na.rm = TRUE))

print(newdata)
print(newdata2)

我对两个数据帧都得到了不同的结果。 “newdata”中的所需结果。你能告诉我为什么我得到这两个计算的不同值? 我需要括号用于更复杂的自定义函数,但它似乎写出了整个数据帧的均值,我希望得到每个组的均值。 如何正确使用[]或[[]]?

2 个答案:

答案 0 :(得分:0)

a <- c(1,2,3,4,5,6,7,8,9,10)
b <- rep(c("male", "female"),5)
c <- c(1,2,3,4,5,6,7,8,9,10)


data <- data.frame(cbind(a,b,c))
data$c <- as.numeric(as.character(data$c))

c
data$c

print(newdata)
print(newdata2)

newdata <- data %>%
    group_by(data[[2]]) %>%
    summarise(Mean = mean(c, na.rm = TRUE))

newdata2 <- data %>%
    group_by(data[[2]]) %>%
    summarise(Mean = mean(data[[3]], na.rm = TRUE))

newdata
newdata2

更新了代码,结果仍然不同:(

答案 1 :(得分:0)

Gewicht&lt; - aggregate(data [[varGewicht]],by = list(data [[varx]]),FUN = mean,na.rm = TRUE)

聚合函数有效: - )