簡體   English   中英

通過 r 中的后綴從數據幀中刪除向量

[英]removing vectors from data frame by suffix in r

數據框中的have向量包含后綴_rc_1 我想從數據框中刪除這些向量。 我已經嘗試了幾個選項,但得到的錯誤表明我誤解了一些東西。 例如:

library(dplyr)
newdata <- subset(mydata, -contains("_rc_1"))
Error: No tidyselect variables were registered

我不知道我如何解決這個問題。

也許這最好用grepl()和正則表達式來完成,但我正在努力實現一個在這里也能按計划執行的版本。

contains work with dplyr If we need to use subset (a base R function), use grep which can take regex pattern and return either a numeric index or the column names itself as select argument in subset can take both as valid inputs

subset(mydata, select = grep("_rc_1", names(mydata), value = TRUE, invert = TRUE))

此外,在base R中有用於前綴/后綴匹配的startsWith/endsWith

subset(mydata, select = names(mydata)[!endsWith(names(mydata), "_rc_1")])

dplyr中, select_helpers - containsselect一起使用

library(dplyr)
mydata %>%
   select(-contains("_rc_1"))

可使用內置數據集“iris”重現

data(iris)
head(subset(iris, select = names(iris)[!endsWith(names(iris), "Length")]))
iris %>%  
    select(-contains('Sepal')) %>%
    head

在基礎 R 中,您可以使用grepl獲取長度等於 ncol ncol(mydata)的邏輯向量,對於以_rc_1結尾的列名,該向量為TRUE ($ 確保 _rc_1 出現在末尾)。 然后將TRUEFALSE交換為! ,您可以使用[]對數據框進行子集化。

newdata <- mydata[!grepl('_rc_1$', names(mydata))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM