請考慮這個水果的例子一句:功能自動寫入給定R中的向量和條件
canada <- c(100, 80, 100, 100, 100, 100, 80, 100, 100)
korea <- c(100, 30, 100, 100, 100, 100, 30, 100, 100)
brazil <- c(100, 100, 100, 30, 100, 100, 100, 30, 100)
fruit <- rbind(canada, korea, brazil)
colnames(fruit) <- c("apple", "orange", "banana", "kiwi", "grape", "monkfruit", "strawberry", "melon", "pineapple")
而且,以下前提:
1)我不知道平均價格爲每柱。
2)我將每個水果價格與列平均值進行比較。
我想創建下面的句子:
> price("korea")
> Every fruit costs above average except orange and strawberry.
所以,這是我試過到目前爲止:
price <- function(val){
# General Functions ----
val <- tolower(val)
myrow <- fruit[val,]
nation <- tools::toTitleCase(val)
name.max <- names(myrow)[which.max(c(myrow))]
name.min <- names(myrow)[which.min(c(myrow))]
score.max <- c(myrow)[which.max(c(myrow))]
score.min <- c(myrow)[which.min(c(myrow))]
if (score.min < mean(fruit[, name.min])
)
cat("This is not exactly the right way to pull only the score that are below average because it only prints the minimum score.")
}
是否有一個快速的命令,我可以代替which.min的使用?
對於給定的例子,你的期望輸出是什麼? –
嗨Ronak,我編輯了我的問題。我想輸入「價格(」korea「)」或任何一個國家,R會返回不超過平均成本的水果名稱。 –
'colnames(fruit)[fruit [「korea」,]> colMeans(fruit)]'你想要的結果? – thelatemail