0
在給定條件的數據框中計算列的子集的平均值/中值時,遇到了一個有趣的問題。Dplyr select() - 需要數值數據問題
在這一點上,我並不需要建議,但認爲這個問題將是有趣的分享:(。)
testFile <- data.frame(
ID = 1:10, Value = rnorm(10),
Condition = sample(c("Yes", "No"), 10, replace = T)
)
median1 <- median(testFile[testFile$Condition == "Yes", ]$Value)
median2 <- testFile %>%
filter(Condition == "Yes") %>%
select(Value) %>%
median
錯誤median.default:需要數字數據
但是,下面的代碼工作:
max2 <- testFile %>%
filter(Condition == "Yes") %>%
select(Value) %>%
max
values <- testFile %>%
filter(Condition == "Yes") %>%
select(Value)
class(values[2,1])
使用[1] 「數字」