Select column of a data.frame --- Database Division in R

Asked

Viewed 11,355 times

3

I imported a table as a database to handle in R. However, I need to do some calculations with just a few columns of this table.

How do I select only these columns for calculations?

2 answers

5


There are several ways to select columns from a data.frame in R, let’s use as an example the data.frame mtcars. To find out which columns exist, you can ask to see the names or colnames of data.frame:

names(mtcars)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

To select any of these columns, for example, the column mpg, you can use the $mpg, the brackets [,"mpg"] as if it were a matrix, or double brackets as if it were a list [["mpg"]]:

mtcars$mpg
mtcars[, "mpg"]
mtcars[["mpg"]]

These three mentioned forms return a vector as a result. You can also select a date.frame containing the column mpg (note the difference, you get a data.frame and not an array). For that you will use the simple bracket as if it were a list:

mtcars["mpg"]

Or also use the matrix were, with the argument drop = FALSE.

mtcars[ ,"mpg", drop = FALSE]

If you want to select more than one column, you can use both the simple bracket and the simple bracket as an array.

mtcars[ ,c("mpg", "cyl")] # seleciona duas colunas
mtcars[c("mpg", "cyl")] # seleciona duas colunas

Note that now the matrix form returns a data.frame, since you are selecting more than one column. There are convenience functions to do this too, such as function subset that Rafael mentioned. She will return you a data.frame with the column mpg and not a vector:

subset(mtcars, select = c("mpg","cyl"))

And each data manipulation package also has its own way of selecting columns. For example, the dplyr has the select function, which is very similar to the subset mentioned:

mtcars %>% select(mpg, cyl)

1

It would be easier if you included your database (or some part of it) so that we could work on it. Take a look at the function dput to that end. It would also be nice if you included the code you developed / tried to develop.

As to your doubt, the functions subset, of the very basis of R, or the function select, package dplyr, should assist you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.