相关疑难解决方法(0)

如果任何列包含特定字符串,则删除行

我试图找出R中最好的方法来删除包含特定字符串的行,在我的情况下是'no_data'.

我有来自外部来源的数据,用'no_data'来判断na的

一个例子是这样的:

 time  |speed  |wheels
1:00   |30     |no_data
2:00   |no_data|18
no_data|no_data|no_data
3:00   |50     |18
Run Code Online (Sandbox Code Playgroud)

我想查看数据并删除任何列中包含此"no_data"字符串的每一行.我很难搞清楚这一点.我尝试了一个sapply,filter,grep以及三者的组合.我绝不是一个专家,所以它可能只是我错误地使用这些.任何帮助,将不胜感激.

r

12
推荐指数
3
解决办法
5385
查看次数

使用dplyr和stringr检测多个字符串

我正在尝试将dplyr和stringr结合起来检测数据帧中的多个模式.我想使用dplyr,因为我想测试许多不同的列.

这是一些示例数据:

test.data <- data.frame(item = c("Apple", "Bear", "Orange", "Pear", "Two Apples"))
fruit <- c("Apple", "Orange", "Pear")
test.data
        item
1      Apple
2       Bear
3     Orange
4       Pear
5 Two Apples
Run Code Online (Sandbox Code Playgroud)

我想用的是:

test.data <- test.data %>% mutate(is.fruit = str_detect(item, fruit))
Run Code Online (Sandbox Code Playgroud)

并收到

        item is.fruit
1      Apple        1
2       Bear        0
3     Orange        1
4       Pear        1
5 Two Apples        1
Run Code Online (Sandbox Code Playgroud)

一个非常简单的测试工作

> str_detect("Apple", fruit)
[1]  TRUE FALSE FALSE
> str_detect("Bear", fruit)
[1] FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)

但即使没有dplyr,我也无法在数据框的列上工作:

> test.data$is.fruit <- str_detect(test.data$item, fruit)
Error …
Run Code Online (Sandbox Code Playgroud)

r stringr dplyr

11
推荐指数
2
解决办法
1万
查看次数

dplyr / tidy 方法来过滤基于子字符串的向量?

我们可以看到一些关于如何基于子字符串过滤 data.frame 的好例子;有没有一种整洁的方法来为向量做这件事?(即不使用grepl()或类似)

例子

我尝试了对 data.frame 有效的方法

# Leave only words that don't begin with 'cat'

vec <- c("cat", "catamaran", "dog", "mouse", "catacombs")

vec %>% filter(substr(1, 3) != "cat") # %>% ... etc
Run Code Online (Sandbox Code Playgroud)

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "character"
Run Code Online (Sandbox Code Playgroud)

笔记

我们可以使用类似的东西vec %>% { .[!grepl("cat", .)] },或者更准确地说vec %>% { .[substr(., 1, 3) != "cat"]},但我会尝试找到一些东西......

  1. 对初学者更友好,具有更多的口头描述功能(例如,一个完整的新手可能会猜到“过滤器”的作用,但可能不会猜到“grepl”)
  2. 少挑剔的语法(尽可能少{ …

r dplyr

5
推荐指数
3
解决办法
382
查看次数

将 filter_all(any_vars()) 转换为 filter(across())

在更新我自己对另一个线程的答案时,我无法想出一个好的解决方案来替换最后一个示例(见下文)。这个想法是获取任何列包含某个字符串的所有行,在我的示例“V”中。

library(tidyverse)

#get all rows where any column contains 'V'
diamonds %>%
  filter_all(any_vars(grepl('V',.))) %>%
  head
#> # A tibble: 6 x 10
#>   carat cut       color clarity depth table price     x     y     z
#>   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
#> 2 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
#> 3 0.24  Very Good J     VVS2     62.8 …
Run Code Online (Sandbox Code Playgroud)

r dplyr

5
推荐指数
1
解决办法
516
查看次数

如何在行而不是列上应用 dplyr 的 select(,starts_with()) ?

我现在试图找出一种方法来选择在变量或特定字母中具有特定值的数据,尤其是使用与 starts_with() 类似的算法。

假设我有一个名为“学校”的数据,如下所示:

Name Math English
James 80   90
Tom   91   91
Shaun 99   71
Jack  92   91
Run Code Online (Sandbox Code Playgroud)

在这里, select(school, starts_with("M")) 只给我列“数学”。我想在行上应用它,例如命令'给我名称以“J”字母开头的行',这又会给我一个两行的数据。

我尝试转置数据并成功实现了我想要的结果,但这并不是我真正想要的。

我怎样才能完成这项工作?

select r

2
推荐指数
2
解决办法
1万
查看次数

基于字符串条件过滤行,dplyr过滤器,包含

我想使用 dplyr contains() 和过滤器过滤数据帧。一定很简单吧?我见过的例子使用了基本的 R grepl ,它在某种程度上击败了对象。这是一个简单的数据框:

site_type <- c('Urban','Rural','Rural Background','Urban Background','Roadside','Kerbside')
df <- data.frame(row_id, site_type)
df <- as.tibble(df)
df
Run Code Online (Sandbox Code Playgroud)

现在我想按 site.type 包含字符串背景的所有行过滤数据框。如果我知道 site_type 的唯一值,我可以直接找到该字符串:

filtered_df <- filter(df, site_type == 'Urban Background')

但我想做一些类似的事情:

filtered_df <- filter(df, site_type(contains('background', match_case = False)))

有什么想法如何做到这一点吗?dplyr 助手contains只能用于列而不是行吗?

r contains filter dplyr

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

r ×6

dplyr ×4

contains ×1

filter ×1

select ×1

stringr ×1