Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all columns in dplyr/sql?

Tags:

mysql

r

dplyr

I am connecting a MySQL data base with dplyr and handle the data with dplyr and the %>% operatot subsequently.

conDplyr <- src_mysql(user = db_user, password = db_pw, dbname = db_name, host = db_host, port = some_port)

As long as I select a certain number of columns but all, it works!

dat <- conDplyr %>%
 tbl('table_name') %>%
 select(c1, c2, c3, c4) %>%
 filter(!is.null(c4))

Now, I ran over a use case, in which I need to select all columns (whole table). All tutorials I found (about dplyr) handled this by selecting the whole dataframe (which I do not have)

some_dataframe <- ...
select(some_dataframe)

I have not found any suggestions in combination with databases. Perhaps the day was too long. Does anybody could help me please?

Best Rob

conDplyr <- src_mysql(...)
dat <- conDplyr %>%
tbl('table_name') %>%
select(everything()) %>%
filter(!is.null(ean))

works just fine. Thanks! If I leave out the select the query results in an error (non-defined columns selected). Again, I did not just want to work with a already existing data frame, but with a table queried from a database, that`s why I am have to do, correct me if I am wrong, an select.

like image 835
Robert Kirsten Avatar asked Aug 25 '15 16:08

Robert Kirsten


People also ask

How do you select all columns in R studio?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.

How do I select data in R dplyr?

Select Variables by Index PositionThe select() function of dplyr package is used to select variable names from the R data frame. Use this function if you wanted to select the data frame variables by index or position.

Is dplyr similar to SQL?

dplyr is a R package that provides a set of grammar based functions to transform data. Compared to using SQL, it's much easier to construct and much easier to read what's constructed.


1 Answers

As @AntoniosK pointed out I do not know why you would like to do that. However, have you tried everything?:

some_dataframe <- ...
select(everything())

For instance:

select(iris, everything()) # or
iris %>% select(everything())
like image 121
mpalanco Avatar answered Sep 24 '22 15:09

mpalanco