Nicholas
Nicholas

Reputation: 3737

Creating a data.frame with piping in R - and naming columns

I know I can do this in other ways, but I am just curious.

dfDice = sample(1:6, 10000, replace = TRUE) %>%
            data.frame()

The above creates a data.frame, where the column header is called '.'.

So my first question is can I pipe the column header into my code? I have tried putting it in my data.frame() function but it just creates a new column.

And my second question is, can I pipe multiple columns into a data.frame, or would I have to do something like this?:

dfDice = (num = sample(1:6, 10000, replace = TRUE) %>% 
              data.frame(letters = sample(LETTERS, 10000, replace = TRUE))

Again, I know this is not the best way to create a data.frame, I am just curious from a learning perspective and trying to fully understand piping.

Upvotes: 2

Views: 2792

Answers (2)

Robin Gertenbach
Robin Gertenbach

Reputation: 10786

So my first question is can I pipe the column header into my code? I have tried putting it in my data.frame() function but it just creates a new column.

For single columns you have two options:

dfDice <- sample(1:6, 10000, replace = TRUE) %>%
  data.frame() %>%
  setNnames("num")

dfDice <- sample(1:6, 10000, replace = TRUE) %>%
  data.frame(num = .)

And my second question is, can I pipe multiple columns into a data.frame?

sample(1:6, 5, replace = TRUE) %>% 
  cbind(sample(LETTERS, 5, replace = TRUE)) %>% 
  as.data.frame() %>% 
  setNames(c("num", "letters"))

Upvotes: 3

Ronak Shah
Ronak Shah

Reputation: 389045

To assign names, you can use a predefined vector and use setNames

library(dplyr)

cols <- "a"

sample(1:6, 10, replace = TRUE) %>%
   data.frame()  %>%
   setNames(cols)

Or can also name dynamically without knowing number of columns beforehand.

sample(1:6, 10, replace = TRUE) %>%
  data.frame()  %>%
  setNames(letters[seq_along(.)])

For 2nd question simplest option would be

data.frame(a = sample(1:6, 10, replace = TRUE), 
           b = sample(LETTERS, 10, replace = TRUE))

OR if you want to use piping maybe ?

sample(1:6, 10, replace = TRUE) %>% 
  data.frame() %>%
  setNames(cols) %>%
  bind_cols(b = sample(LETTERS, 10, replace = TRUE))

Upvotes: 2

Related Questions