add_variables()
adds a new column to a data frame, while
add_cases()
adds a new row to a data frame. These are convenient
functions to add columns or rows not only at the end of a data frame,
but at any column or row position. Furthermore, they allow easy integration
into a pipe-workflow.
add_variables(data, ..., .after = Inf, .before = NULL) add_case(data, ..., .after = Inf, .before = NULL)
data | A data frame. For |
---|---|
... | One or more names vectors that indicate the variables or values,
which will be added as new column or row to |
.after, .before | Numerical index of row or column, after or infront of what
the new variable or case should be added. If |
data
, including the new variables or cases from ...
.
For add_case()
, if variable does not exist, a new variable is
created and existing cases for this new variable get the value NA
.
See 'Examples'.
d <- data.frame( a = c(1, 2, 3), b = c("a", "b", "c"), c = c(10, 20, 30), stringsAsFactors = FALSE ) add_case(d, b = "d")#> a b c #> 1 1 a 10 #> 2 2 b 20 #> 3 3 c 30 #> 4 NA d NAadd_case(d, b = "d", a = 5, .before = 1)#> a b c #> 4 5 d NA #> 1 1 a 10 #> 2 2 b 20 #> 3 3 c 30# adding a new case for a new variable add_case(d, e = "new case")#> a b c e #> 1 1 a 10 <NA> #> 2 2 b 20 <NA> #> 3 3 c 30 <NA> #> 4 NA <NA> NA new caseadd_variables(d, new = 5)#> a b c new #> 1 1 a 10 5 #> 2 2 b 20 5 #> 3 3 c 30 5add_variables(d, new = c(4, 4, 4), new2 = c(5, 5, 5), .after = "b")#> a b new new2 c #> 1 1 a 4 5 10 #> 2 2 b 4 5 20 #> 3 3 c 4 5 30