Извлечь столбцы, содержащие определенное слово - PullRequest
0 голосов
/ 18 июня 2019

Мой файл выглядит так -

ball    cat     bird    ball    cat     cat     ball
apple   mouse   apple   apple   mouse   mouse   apple
cat     bat     mouse   cat      bat    bat     cat
mouse   ball    bat     ball    ball    ball    ball
bat     ball    mouse   bat     bat      bat    bat
bird    ball    ball    bird    bird    bird    bird

Я хочу извлечь те столбцы, которые содержат слово «яблоко»

Ожидаемый результат -

ball    bird    ball    ball
apple   apple   apple   apple
cat     mouse   cat     cat
mouse   bat     ball    ball
bat     mouse   bat     bat
bird    ball    bird    bird

Ответы [ 3 ]

2 голосов
/ 18 июня 2019

Так много способов сделать это, также я думаю, что на это нужно где-то ответить

1) Используя colSums

df[colSums(df == "apple") > 0]

#     V1    V3    V4    V7
#1  ball  bird  ball  ball
#2 apple apple apple apple
#3   cat mouse   cat   cat
#4 mouse   bat  ball  ball
#5   bat mouse   bat   bat
#6  bird  ball  bird  bird

2) с apply

df[apply(df == "apple", 2, any)]

3) Использование Filter

Filter(function(x) any(x == "apple"), df)

4) dplyr

library(dplyr)
df %>% select_if(~any(. == "apple"))

data

df <- structure(list(V1 = structure(c(2L, 1L, 5L, 6L, 3L, 4L), .Label = 
c("apple", 
"ball", "bat", "bird", "cat", "mouse"), class = "factor"), V2 = 
structure(c(3L, 
4L, 2L, 1L, 1L, 1L), .Label = c("ball", "bat", "cat", "mouse"
), class = "factor"), V3 = structure(c(4L, 1L, 5L, 3L, 5L, 2L
), .Label = c("apple", "ball", "bat", "bird", "mouse"), class = "factor"), 
V4 = structure(c(2L, 1L, 5L, 2L, 3L, 4L), .Label = c("apple", 
"ball", "bat", "bird", "cat"), class = "factor"), V5 = structure(c(4L, 
5L, 2L, 1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat", 
"mouse"), class = "factor"), V6 = structure(c(4L, 5L, 2L, 
1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat", "mouse"
), class = "factor"), V7 = structure(c(2L, 1L, 5L, 2L, 3L, 
4L), .Label = c("apple", "ball", "bat", "bird", "cat"), class = "factor")), 
class = "data.frame", row.names = c(NA, -6L))
1 голос
/ 18 июня 2019

Мы можем использовать sapply от base R

df[sapply(df, function(x)  'apple' %in% x)]

данные

df <- structure(list(V1 = structure(c(2L, 1L, 5L, 6L, 3L, 4L), .Label = c("apple", 
"ball", "bat", "bird", "cat", "mouse"), class = "factor"), V2 = structure(c(3L, 
4L, 2L, 1L, 1L, 1L), .Label = c("ball", "bat", "cat", "mouse"
), class = "factor"), V3 = structure(c(4L, 1L, 5L, 3L, 5L, 2L
), .Label = c("apple", "ball", "bat", "bird", "mouse"), class = "factor"), 
    V4 = structure(c(2L, 1L, 5L, 2L, 3L, 4L), .Label = c("apple", 
    "ball", "bat", "bird", "cat"), class = "factor"), V5 = structure(c(4L, 
    5L, 2L, 1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat", 
    "mouse"), class = "factor"), V6 = structure(c(4L, 5L, 2L, 
    1L, 2L, 3L), .Label = c("ball", "bat", "bird", "cat", "mouse"
    ), class = "factor"), V7 = structure(c(2L, 1L, 5L, 2L, 3L, 
    4L), .Label = c("apple", "ball", "bat", "bird", "cat"), 
    class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))
0 голосов
/ 18 июня 2019

Если столбцы могут содержать большое количество различных значений, и время обработки или производительность становятся проблемой, вы можете сначала привести столбцы как факторы и искать совпадения на уровнях, а не выполнять итерацию по всему набору данных.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...