R语言 仅当某些列之间的任何值满足条件[已关闭]时才保留行

km0tfn4u  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(142)

**关闭。**这个问题是not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
2天前关闭。
Improve this question
我正在寻找一种方法来子集我的dataframe。它是92列和大约2000行。我只需要保留那些(在 Dataframe 的最后50列中)在任何列中包含-30到30之间的值的行。
所有需要满足此条件的列都以字符串'daysbetween'开头,因此我尝试了以下操作:

data <- crf1 %>%
  filter(if_any(starts_with("daysbetween"), ~ . > -30 &  ~. < 30 ))

但我得到了以下错误:

Error in `filter()`:
ℹ In argument: `... | daysbetweenV1312 > -30 & (~ . < 30)`.
Caused by error in `daysbetweenV21 > -30 & (~ . < 30)`:
! operations are possible only for numeric, logical or complex types

以下是我的数据样本:

structure(list(V1 = c("00006", "00009", "00013", "00017", "00040", 
"00042", "00043", "00051", "00061", "00068"), V2 = structure(c(18750, 
18745, 18753, 18922, 18898, 18925, 19018, 18726, 18977, 18766
), class = "Date"), V3 = structure(c(18917, 18904, 19146, 18922, 
18898, 19018, 19196, 18959, 18946, 18945), class = "Date"), V4 = structure(c(18823, 
18905, 19275, 19187, 19219, 19134, 19331, 18885, 18922, 19275
), class = "Date"), V5 = structure(c(18972, 19185, NA, NA, NA, 
19349, 19289, 19063, 19149, 18400), class = "Date"), V6 = structure(c(19352, 
NA, NA, NA, NA, NA, NA, 19035, 19028, NA), class = "Date"), V7 = structure(c(NA, 
NA, NA, NA, NA, NA, NA, 19197, NA, NA), class = "Date"), V8 = structure(c(NA, 
NA, NA, NA, NA, NA, NA, 19411, NA, NA), class = "Date"), V9 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), V10 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), V11 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), V12 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), V13 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), V14 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), daysbetweenV21 = structure(c(167, 
159, 393, 0, 0, 93, 178, 233, -31, 179), class = "difftime", units = "days"), 
    daysbetweenV22 = structure(c(73, 160, 522, 265, 321, 209, 
    313, 159, -55, 509), class = "difftime", units = "days")), row.names = c(NA, 
10L), class = "data.frame")

列V1到V14不必过滤,只有以“daysbetween”开头的列才是相关的。
先谢谢你了

ny6fqffe

ny6fqffe1#

您添加了一个额外的“~”:

data <- crf1 %>%
  filter(if_any(starts_with("daysbetween"), ~ . > -30 &  . < 30 ))

# V1         V2         V3         V4   V5   V6   V7   V8   V9  V10  V11  V12  V13  V14
# 1 00017 2021-10-22 2021-10-22 2022-07-14 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#   2 00040 2021-09-28 2021-09-28 2022-08-15 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#   daysbetweenV21 daysbetweenV22
# 1         0 days       265 days
# 2         0 days       321 days

展开一点“~”告诉R你正在输入一个匿名函数,所以这个调用只能进行一次,然后你就可以使用。/ .x语法之后没有问题

相关问题