我想通过textInput从仪表板用户处读取多个逗号分隔的年份,该年份可以输入一系列数字(例如1910、1980:1990、2017)。然后,我需要遍历列表中的每一年并将其从数据表中删除。
我的函数如下所示,其中daily_mean_Q是数据帧,excluded_years是来自用户的数组c(1910,1980:1990,2017)。
remove_years <- function(daily_mean_Q, excluded_years) {
daily_mean_Q <- daily_mean_Q %>%
mutate(Year = str_sub(Date, 1, 4))
for(year in excluded_years) {
daily_mean_Q <- daily_mean_Q %>%
filter(Year != as.character(year))
}
daily_mean_Q <- daily_mean_Q %>%
select(-Year)
}
这实际上是Difference between `%in%` and `==`的副本,因为您试图对设置成员身份操作使用相等性,即使您尚未(尚未)尝试%in%
。 (除非我完全误解了您的问题。)
R中向量vec1
和vec2
的基本相等性通过几种方式起作用:
如果vec2
(或vec1
)的长度为1,则将vec1
中的每个与它进行比较,如vec1[1] == vec2[1]
中那样,vec1[2] == vec2
如中]中
1:10 == 3
# [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
如果length(vec1) == length(vec2)
,那么我们碰巧比较是逐元素进行的:
1:10 == c(1, 2, 3, 99, 99, 6, 7, 99, 99, 99)
# [1] TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
如果length(vec1)
长度是length(vec2)
的偶数倍,则R 静默回收
。最终,当我们键入1:10 == c(3, 2) # [1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ### which is effectively 1:10 == c(3, 2, 3, 2, 3, 2, 3, 2, 3, 2) # [1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
到目前为止似乎不错,这是这里[[偶然
1:10 == c(2, 3)
时,我们最终是说vec1
的1、3、5,...元素是2
,而vec1
的第二,4、6,...元素是3
。通常,这不是预期的目的,通常是set-membership的意思。如果它正在执行集合成员身份,那么我们希望反转vec2
中的数字将没有任何作用……但这不是真的。1:10 == c(2, 3)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
### which is effectively
1:10 == c(2, 3, 2, 3, 2, 3, 2, 3, 2, 3)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
length(vec1)
为不是
length(vec2)
的偶数倍,仍然会接近上述值,但至少我们会看到警告:1:10 == c(3, 2, 1)
# Warning in 1:10 == c(3, 2, 1) :
# longer object length is not a multiple of shorter object length
# [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
### which is effectively
1:10 == c(3, 2, 1, 3, 2, 1, 3, 2, 1, 3) # uneven recycling
# [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
总结向量==
的操作,旨在(并且安全!)比较相同长度的向量,或者当向量之一为长度1时进行比较。尽管任何其他条件都可能不会发出警告或错误,但结果通常是不是预期的。
[当您想知道vec1
中包含哪个vec2
时,我们需要%in%
运算符:
1:10 %in% c(2, 3) # [1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ### order in vec2 is not important 1:10 %in% c(3, 2) # [1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
这实际上是对于vec1
中的每个元素,是==
中的任何元素的元素vec2
,实际上是我们上面的第一个项目符号:元素的长度为1,而vec2
为1或者更多。错误的伪代码循环证明了这一点:
for (el in vec1) # el is length 1 if (any(el == vec2)) # this works as intended per bullet 1 above then true else false done
如果您的excluded_years
确实是integer
向量,如
excluded_years <- c(1957, 1960:1970, 1987) excluded_years # [1] 1957 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1987
(从技术上讲,此向量为numeric
,而不是integer
,但我们暂时将其忽略。)然后我们可以对其进行简单过滤:
library(dplyr) filter(mtcars, ! cyl %in% c(4, 8)) # mpg cyl disp hp drat wt qsec vs am gear carb # Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 # Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 # Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 # Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 # Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 # Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 # Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
并且看到数据不再包含cyl
值(仅包括4、6和8)。这样,您可以将函数替换为以下之一:
remove_years <- function(daily_mean_Q, excluded_years) { daily_mean_Q %>% mutate(Year = as.integer(stringr::str_sub(Date, 1, 4))) %>% filter(! Year %in% excluded_years) %>% select(-Year) } remove_years <- function(daily_mean_Q, excluded_years) { daily_mean_Q %>% filter(! as.integer(stringr::str_sub(Date, 1, 4)) %in% excluded_years) }
但是,如果您的excluded_years
是一个字符串,由于shiny
字段倾向于返回,那么我们有一些选择来转换它:
excluded_years <- "1957, 1960:1970, 1987"
eval(parse(text = paste("c(", excluded_years, ")")))
# [1] 1957 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1987
### PROBLEM
excluded_years <- "1957, 1960:1970); message('gotcha'); c("
eval(parse(text = paste("c(", excluded_years, ")")))
# gotcha
# NULL
excluded_years <- "1957, 1960:1970, 1987"
strsplit(excluded_years, "[, ]+")
# [[1]]
# [1] "1957" "1960:1970" "1987"
unlist(lapply(strsplit(excluded_years, "[, ]+")[[1]],
function(a) {
a <- strsplit(a, "[: ]+")[[1]]
if (length(a) == 1) return(as.integer(a))
if (length(a) == 2) return(seq(a[1], a[2]))
stop("unrecognized sequence");
}))
# [1] 1957 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1987