[英]Removing rows from dataframe that contains string in a particular column
所以我在 R 中清理一個巨大的數據文件,一個例子如下所示:
ID Score
1001 4
1002 2
1003 h
1004 v
1005 3
因為 Score 列的類是“字符”,所以我想使用 as.numeric 函數將 4,20 和 30 轉換為數值。 但是由於數據是臟的(包含不合理的字符串,如 h、v),我收到消息:
NAs introduced by coercion.
當我運行以下命令時:
as.numeric(df$Score)
所以我現在想要做的是刪除包含帶字母的字符串的數據框中的行,以便我獲得:
ID Score
1001 4
1002 2
1005 3
有多種方法可以做到這一點:
轉換為數字並刪除NA
值
subset(df, !is.na(as.numeric(Score)))
# ID Score
#1 1001 4
#2 1002 20
#5 1005 30
或者使用grepl
查找其中是否有任何非數字字符並將其刪除
subset(df, !grepl('\\D', Score))
這也可以用grep
來完成。
df[grep('\\D', df$Score, invert = TRUE), ]
數據
df <- structure(list(ID = 1001:1005, Score = c("4", "20", "h", "v",
"30")), class = "data.frame", row.names = c(NA, -5L))
您可以使用str_detect
在tidyverse
包,如下所示:
df[str_detect(df$Score, "\\d"),]
或者
df %>% filter(str_detect(df$Score, "\\d"))
兩者都產生輸出:
# ID Score
#1 1001 4
#2 1002 20
#5 1005 30
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.