Shuo
Shuo

Reputation: 491

R subset a list with conditional statement

Suppose I have a column variable which is comprised of 3 sets of variable of the same length.

x <- sample(1:10, 10, replace = T)
y <- sample(c("$","#","!"), 10, replace = T)
z <- sample(LETTERS, 10, replace = T)
column <- list(value = x, varA = y, varB = z)

> column
$value
 [1] 5 6 9 1 9 3 3 5 4 4

$varA
 [1] "$" "$" "!" "$" "!" "$" "#" "!" "!" "!"

$varB
 [1] "H" "V" "V" "S" "W" "K" "K" "Q" "T" "D"

Suppose I want to filter out all sets of values where value > 5.

In the example above, the return would be

$value
 [1] 5 6 9 9 5

$varA
 [1] "$" "$" "!" "!" "!"

$varB
 [1] "H" "V" "V" "W" "Q"

Is there a simple way to do this? Or is using a loop the only way to subset across columns?

Upvotes: 1

Views: 3535

Answers (2)

BrodieG
BrodieG

Reputation: 52637

If all your variables have the same length, as must be the case for your question to make sense, then you should consider using a data frames, which then allows you to use functions such as subset to filter all columns on one condition:

> DF <- as.data.frame(column, stringsAsFactors=F)
> (DF.small <- subset(DF, value >= 5))
  value varA varB
2     8    #    X
3     6    $    H
5     7    #    J
6     6    !    H
7     7    $    F
8     9    $    I
9     7    !    E
> as.list(DF.small)
$value
[1] 8 6 7 6 7 9 7

$varA
[1] "#" "$" "#" "!" "$" "$" "!"

$varB
[1] "X" "H" "J" "H" "F" "I" "E"

Note this won't match exactly to yours because you didn't set a seed.

Upvotes: 4

akrun
akrun

Reputation: 886938

Try

lapply(column, `[`, column$value >=5)
#   $value
#[1] 5 6 9 9 5

#$varA
#[1] "$" "$" "!" "!" "!"

#$varB
#[1] "H" "V" "V" "W" "Q"

Or

Map(`[` , column, list(column$value>=5))

data

column <- structure(list(value = c(5, 6, 9, 1, 9, 3, 3, 5, 4, 4), varA = c("$", 
"$", "!", "$", "!", "$", "#", "!", "!", "!"), varB = c("H", "V", 
"V", "S", "W", "K", "K", "Q", "T", "D")), .Names = c("value", 
"varA", "varB"))

Upvotes: 3

Related Questions