How to filter / multiply data.frame using values โ€‹โ€‹from one of the columns

How can I "crop" data.frame based on values โ€‹โ€‹in a single column? For example, if I have this matrix

x <- c(5,1,3,2,4)
y <- c(1,5,3,4,2)
data <- data.frame(x,y)

and I want all the data for the values โ€‹โ€‹to be greater than or equal to x, how would I do that? I know that I can find the addresses of x values โ€‹โ€‹using

addresses <- which(x>=2)

but I'm not sure how to use this to create a new matrix. The following do not work:

data2 <- data[x>=2]
data2 <- data[which(x>=2)]

If anyone can offer any advice, I would really appreciate it.

+5
source share
2 answers

You do not read enough error messages. Here, our error message tells you that you have not selected any columns. You specified a condition for the strings, though ....

> data[which(x>=2)]
Error in `[.data.frame`(data, which(x >= 2)) : undefined columns selected

, (, ), .

> data[which(x>=2), ] # if x is in your workspace
  x y
1 5 1
3 3 3
4 2 4
5 4 2
> ## with(data, data[x >= 2, ] # if x is not in your workspace

: data.frame :

data <- data.frame(x = c(5,1,3,2,4), y = c(1,5,3,4,2))

. -, . -, , , - , . : " , x- addresses <- which(x>=2)". , , , , ( ), , "x" data.frame, "x" .

+13

-, data , . , , . , [. - :

data2 <- data[data$x>=2,]

, , . " ".

+6

All Articles