Reputation: 4815
I want a variable containing all the rows of dataframe df
where x
is greater than y
. But for some reason I keep getting this strange error.
> x <- c(5,6,7,8,9,10)
> y <- c(1,7,8,29,0,1)
>
> df <- data.frame(x, y)
>
> x.is.more <- df[,"x" > "y"]; x.is.more
data frame with 0 columns and 6 rows
The code above should the same results as x.is.more <- df[c(1,6),]
but for some reason it isn't working.
Upvotes: 4
Views: 63
Reputation: 56004
Try this:
#dummy dataframe
df <- data.frame(x=c(5,6,7,8,9,10),
y=c(1,7,8,29,0,1))
#find x>y
x.is.more <- df[df$x > df$y, ]
#output
x.is.more
# x y
#1 5 1
#5 9 0
#6 10 1
Upvotes: 1
Reputation: 4815
Aha! I figured it out.
Instead of,
x.is.more <- df[,"x" > "y"]
I should have,
x.is.more <- df[x > y,]
Upvotes: 0
Reputation: 66834
subset
is probably the most straightforward way of doing this:
subset(df, x>y)
x y
1 5 1
5 9 0
6 10 1
Upvotes: 3
Reputation: 61154
You can use transform
if you want to append the new variable to the existing data frame
> transform(df, x.is.more= x>y)
x y x.is.more
1 5 1 TRUE
2 6 7 FALSE
3 7 8 FALSE
4 8 29 FALSE
5 9 0 TRUE
6 10 1 TRUE
Use with
if you just want a new variable
> x.is.more <- with(df, x>y)
> x.is.more
[1] TRUE FALSE FALSE FALSE TRUE TRUE
If you want to subset then try
> df[ with(df, x>y), ] # equivalent to df[ x.is.more, ]
x y
1 5 1
5 9 0
6 10 1
Or directly use subset
function as in @James answer
Upvotes: 2