2
library(tidyverse)
df <- tibble(col1 = c(5, 2), col2 = c(6, 4), col3 = c(9, 9))
# # A tibble: 2 x 3
#    col1  col2  col3
#   <dbl> <dbl> <dbl>
# 1     5     6     9
# 2     2     4     9

I need to add columns 1 and 3. But the column names often change. So I can only use column numbers as opposed to the actual column name.

Attempt 1 works as expected.
Attempt 2 and 3 don't work.

What's wrong with my syntax? I can't use attempt 1 because next month the column names may be something else, but their relative positions will remain the same.

df %>% mutate(col4 = col1 + col3)        # attempt 1
df %>% mutate(col4 = .[, 1] + .[, 3])    # attempt 2
df %>% {mutate(col4 = .[, 1] + .[, 3])}  # attempt 3
0

1 Answer 1

2

If it is based on position, use rowSums by subsetting the columns based on the column index. The advantage is that we can also take care of NA elements (if any)

df %>% 
  mutate(col4 = rowSums(.[c(1, 3)], na.rm = TRUE))
# A tibble: 2 x 4
#   col1  col2  col3  col4
#  <dbl> <dbl> <dbl> <dbl>
#1     5     6     9    14
#2     2     4     9    11

Regarding the issue in OP's case, we need [[ instead of [ for subsetting a single column as a vector. With df[, 1] or .[,1] it would still be a tibble with one column instead of converting to a vector as we think about the behavior we find with data.frame

df %>% 
     mutate(col4 = .[[1]] + .[[3]])
# A tibble: 2 x 4
#   col1  col2  col3  col4
#  <dbl> <dbl> <dbl> <dbl>
#1     5     6     9    14
#2     2     4     9    11
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.