Home > Back-end >  Creating a colum based on another column while creating a data frame
Creating a colum based on another column while creating a data frame

Time:11-13

I know there a several ways to create a column based on another column, however I would like to know how to do it while creating a data frame.

For example this works but is not the way I want to use it.

v1 = rnorm(10)

sample_df <- data.frame(v1 = v1,
                        cs = cumsum(v1))

This works not:

sample_df2 <- data.frame(v2 = rnorm(10),
                         cs = cumsum(v2))

Is there a way to it directly in the data.frame function? Thanks in advance.

CodePudding user response:

It cannot be done using data.frame, but package tibble implements a data.frame analogue with the functionality that you want.

library("tibble")
tib <- tibble(x = 1:6, y = cumsum(x))
tib
# # A tibble: 6 × 2
#       x     y
#   <int> <int>
# 1     1     1
# 2     2     3
# 3     3     6
# 4     4    10
# 5     5    15
# 6     6    21

In most cases, the resulting object (called a "tibble") can be treated as if it were a data frame, but if you truly need a data frame, then you can do this:

dat <- as.data.frame(tib)
dat
#   x  y
# 1 1  1
# 2 2  3
# 3 3  6
# 4 4 10
# 5 5 15
# 6 6 21

You can wrap everything in a function if you like:

f <- function(...) as.data.frame(tibble(...))
f(x = 1:6, y = cumsum(x))
#   x  y
# 1 1  1
# 2 2  3
# 3 3  6
# 4 4 10
# 5 5 15
# 6 6 21
  • Related