How do you keep the first value in multiple columns?

Here is an option to extract the first numeric part with parse_number on columns that starts with 'E' or 'N' followed by one or more digits (\\d+) in the column name

library(dplyr)
library(stringr)
df %>%
   mutate_at(vars(matches("^(E|N)\\d+$")), ~readr::parse_number(as.character(.)))

Or using str_remove to remove the substring that starts from one or more space including other characters (.*)

df %>%
   mutate_at(vars(-age), ~ str_remove(., "\\s+.*"))
#   age E1 E2 E3 N1 N2 N3
#1  20  1  2  1  1  2  2
#2  25  2  2  2  1  1  2
#3  30  1  2  2  1  2  1

Or using base R

df[-1] <- lapply(df[-1], sub, pattern = "\\s.*", replacement = "")

You can also do:

df %>%
 mutate_at(vars(E1:N3), ~ substr(., 1, 1))

  age E1 E2 E3 N1 N2 N3
1  20  1  2  1  1  2  2
2  25  2  2  2  1  1  2
3  30  1  2  2  1  2  1

Then, if it is always numbers and you are looking for numeric vectors, you can do:

df %>%
 mutate_at(vars(E1:N3), ~ as.numeric(substr(., 1, 1)))

Tags:

R

Dplyr