top5 <- ifelse(state = "PA", c("Oranges", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "MA"), c("Oranges", "Bananas", "Peaches", "Leaves", "Cucumbers"),
ifelse(state = "ME"), c("Oranges", "Bananas", "Avocados", "Tomatoes", "Watermelon"),
ifelse(state = "NH"), c("Tea", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "RI"), c("Oranges", "Coconuts", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "VT"), c("Carrots", "Bananas", "Apples", "Tomatoes", "Cucumbers"))
以上是我现在所拥有的。我想根据输入将相应的向量分配到函数中。我把它放在一个函数中,虽然我甚至无法让它在函数之外工作。谢谢。
回答答案,这是很好的解决方案。但是,我希望将这些向量中的一个传递给基于标准的变量,该变量不起作用。
我需要这样做的原因是稍后调用向量来通过匹配状态的相应向量来过滤该状态的数据。
例如,PA的五大水果是:“橘子”,“香蕉”,“鳄梨”,“西红柿”,“黄瓜”。如果函数调用PA,我需要将其赋值给变量,以便函数稍后可以将数据过滤到那些结果。这是我的完整代码:
FruitAnalyses <- function(statefile,state)
top5 <- ifelse(state = "PA", c("Oranges", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "MA"), c("Oranges", "Bananas", "Peaches", "Leaves", "Cucumbers"),
ifelse(state = "ME"), c("Oranges", "Bananas", "Avocados", "Tomatoes", "Watermelon"),
ifelse(state = "NH"), c("Tea", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "RI"), c("Oranges", "Coconuts", "Avocados", "Tomatoes", "Cucumbers"),
ifelse(state = "VT"), c("Carrots", "Bananas", "Apples", "Tomatoes", "Cucumbers"))
statefile <- fread(paste0(state,"_fruit.csv"), header = T, data.table = F,
stringsAsFactors = F, na.strings = c(",,",""," "))
#Lower names and convert to tbl
names(statefile) <- tolower(names(statefile))
statefile <- as.tbl(statefile)
#Parse Month and Year into single string - This is for later use when
dates <-mdy(statefile$fruitstartdate)
statefile <- statefile %>%
mutate(Month = month(dates, label = T))
rm(dates)
statefileGrouped <- statefile %>%
filter(priorfruitgroup %in% top5) %>%
group_by(priorfruitgroup, Month) %>%
summarise(TotalSeriousFruit = sum(seriousfruit),
TotalFruitCount = sum(fruit)) %>%
arrange(Month)
fwrite(statefileGrouped,file=paste0(state,".csv"))
barplot <- ggplot(statefileGrouped, aes(x=Month, y=Totalfruitcount,
fill=TotalotherfruitCount)) +
geom_bar(color="black",stat="identity") +
facet_grid(.~priorfruitgroup)
return(barplot)
}
忽略变量名称中的差异,出于保密原因,我不得不更改一些名称。然而,这不是问题的主要议题。我想知道为什么top5不会根据标准正确分配给矢量。
答案 0 :(得分:2)
我可能会选择switch
函数:
ifelse
states <- c('PA', 'MA', 'ME', 'NH', 'RI', 'VT')
sapply(states, function(x){
switch(x,
PA = c("Oranges", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
MA = c("Oranges", "Bananas", "Peaches", "Leaves", "Cucumbers"),
ME = c("Oranges", "Bananas", "Avocados", "Tomatoes", "Watermelon"),
NH = c("Tea", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
RI = c("Oranges", "Coconuts", "Avocados", "Tomatoes", "Cucumbers"),
VT = c("Carrots", "Bananas", "Apples", "Tomatoes", "Cucumbers"))
})
PA MA ME NH RI VT
[1,] "Oranges" "Oranges" "Oranges" "Tea" "Oranges" "Carrots"
[2,] "Bananas" "Bananas" "Bananas" "Bananas" "Coconuts" "Bananas"
[3,] "Avocados" "Peaches" "Avocados" "Avocados" "Avocados" "Apples"
[4,] "Tomatoes" "Leaves" "Tomatoes" "Tomatoes" "Tomatoes" "Tomatoes"
[5,] "Cucumbers" "Cucumbers" "Watermelon" "Cucumbers" "Cucumbers" "Cucumbers"
可以按如下方式操纵:
xx <- sapply(states, function(x){
switch(x,
PA = c("Oranges", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
MA = c("Oranges", "Bananas", "Peaches", "Leaves", "Cucumbers"),
ME = c("Oranges", "Bananas", "Avocados", "Tomatoes", "Watermelon"),
NH = c("Tea", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
RI = c("Oranges", "Coconuts", "Avocados", "Tomatoes", "Cucumbers"),
VT = c("Carrots", "Bananas", "Apples", "Tomatoes", "Cucumbers"))
})
apply(t(xx), 1, paste, collapse = ', ')
PA
"Oranges, Bananas, Avocados, Tomatoes, Cucumbers"
MA
"Oranges, Bananas, Peaches, Leaves, Cucumbers"
ME
"Oranges, Bananas, Avocados, Tomatoes, Watermelon"
NH
"Tea, Bananas, Avocados, Tomatoes, Cucumbers"
RI
"Oranges, Coconuts, Avocados, Tomatoes, Cucumbers"
VT
"Carrots, Bananas, Apples, Tomatoes, Cucumbers"
答案 1 :(得分:2)
或者,您可以使用列表。
my.list <- list("PA"=c("Oranges", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
"MA"=c("Oranges", "Bananas", "Peaches", "Leaves", "Cucumbers"),
"ME"= c("Oranges", "Bananas", "Avocados", "Tomatoes", "Watermelon"),
"NH"=c("Tea", "Bananas", "Avocados", "Tomatoes", "Cucumbers"),
"RI"=c("Oranges", "Coconuts", "Avocados", "Tomatoes", "Cucumbers"),
"VT"=c("Carrots", "Bananas", "Apples", "Tomatoes", "Cucumbers"))
然后可以这样访问:
my.list$NH
[1] "Tea" "Bananas" "Avocados" "Tomatoes" "Cucumbers"
或:
x <- 'PA'
my.list[[x]]
使用变量时。