Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to get a dropped column in dplyr tbl_df

Tags:

dataframe

r

dplyr

I remember a comment on r-help in 2001 saying that drop = TRUE in [.data.frame was the worst design decision in R history.

dplyr corrects that and does not drop implicitly. When trying to convert old code to dplyr style, this introduces some nasty bugs when d[, 1] or d[1] is assumed a vector.

My current workaround uses unlist as shown below to obtain a 1-column vector. Any better ideas?

library(dplyr)

d2 = data.frame(x = 1:5, y = (1:5) ^ 2)
str(d2[,1]) # implicit drop = TRUE
# int [1:5] 1 2 3 4 5

str(d2[,1, drop = FALSE])
# data.frame':  5 obs. of  1 variable:
#  $ x: int  1 2 3 4 5

# With dplyr functions
d1 = data_frame(x = 1:5, y = x ^ 2)
str(d1[,1])
# Classes ‘tbl_df’ and 'data.frame':    5 obs. of  1 variable:
#  $ x: int  1 2 3 4 5

str(unlist(d1[,1]))
# This ugly construct gives the same as str(d2[,1])
str(d1[,1][[1]])
like image 911
Dieter Menne Avatar asked Jun 11 '15 13:06

Dieter Menne


People also ask

How do I drop a column in R using dplyr?

Deleting a column using dplyr is very easy using the select() function and the - sign. For example, if you want to remove the columns “X” and “Y” you'd do like this: select(Your_Dataframe, -c(X, Y)) .

How do I remove a specific column in R?

To remove a single column or multiple columns in R DataFrame use square bracket notation [] or use functions from third-party packages like dplyr.

How do I remove two columns in R?

We can delete multiple columns in the R dataframe by assigning null values through the list() function.

How do I exclude a variable in R?

To select variables from a dataset you can use this function dt[,c("x","y")] , where dt is the name of dataset and “x” and “y” name of vaiables. To exclude variables from dataset, use same function but with the sign - before the colon number like dt[,c(-x,-y)] .


1 Answers

You can just use the [[ extract function instead of [.

d1[[1]]
## [1] 1 2 3 4 5

If you use a lot of piping with dplyr, you may also want to use the convenience functions extract and extract2 from the magrittr package:

d1 %>% magrittr::extract(1) %>% str
## Classes ‘tbl_df’ and 'data.frame':  5 obs. of  1 variable:
##   $ x: int  1 2 3 4 5
d1 %>% magrittr::extract2(1) %>% str
##  int [1:5] 1 2 3 4 5

Or if extract is too verbose for you, you can just use [ directly in the pipe:

d1 %>% `[`(1) %>% str
## Classes ‘tbl_df’ and 'data.frame':  5 obs. of  1 variable:
##   $ x: int  1 2 3 4 5
d1 %>% `[[`(1) %>% str
##  int [1:5] 1 2 3 4 5
like image 53
shadow Avatar answered Oct 04 '22 15:10

shadow