Error with mutate(across())

Hi there,

I have a dataframe of character modes and I'm trying to turn them all into numeric. However, I keep getting this error:

Error: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Columns `5.42`, `5.01`, `4.73`, `4.52`, `4.24`, etc. don't exist.
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

I have a dataset where I remove the first row because it contains units. I then use the mutate(across()) function to transform them the columns into numeric modes with the following code:

dt <- dt[-1,]
dt <- dt %>% mutate(across(.cols = dt$Green.Algae:dt$Temp..Sample, as.numeric))

That's when I get the error. Can anyone help me? Here's a subset of my data:

dt <- structure(list(Date.Time = c("date", "6/11/2021 8:16", "6/11/2021 8:16", 
"6/11/2021 8:16", "6/11/2021 8:16", "6/11/2021 8:17"), Green.Algae = c("µg/l", 
"5.42", "5.01", "4.73", "4.52", "4.24"), Bluegreen = c("µg/l", 
"0", "0", "0", "0", "0"), Diatoms = c("µg/l", "12.79", "13.7", 
"13.68", "13.14", "12.87"), Cryptophyta = c("µg/l", "0", "0", 
"0", "0", "0"), Yellow.substances = c("r.u.", "2.09", "2.42", 
"2.4", "2.47", "2.48"), Total.conc. = c("µg/l", "18.21", "18.71", 
"18.41", "17.67", "17.11"), Transmission = c("%", "96.17", "96.3", 
"96.48", "96.47", "96.4"), Depth = c("m", "2.05", "2.61", "3.21", 
"3.78", "4.29"), Temp..Sample = c("¡C", "19.68", "19.18", "18.77", 
"18.52", "18.25")), row.names = c(NA, 6L), class = "data.frame")

Thanks so much!!

This is your data. You'll note that the first row is units and should be removed. This is causing some of the issues.

dt %>% 
  as_tibble() %>% 
  mutate(across(Green.Algae:Temp..Sample, as.numeric))

# A tibble: 6 × 10
  Date.Time      Green.Algae Bluegreen Diatoms Cryptophyta Yellow.substances Total.conc. Transmission Depth Temp..Sample
  <chr>          <chr>       <chr>     <chr>   <chr>       <chr>             <chr>       <chr>        <chr> <chr>       
1 date           µg/l        µg/l      µg/l    µg/l        r.u.              µg/l        %            m     ¡C          
2 6/11/2021 8:16 5.42        0         12.79   0           2.09              18.21       96.17        2.05  19.68       
3 6/11/2021 8:16 5.01        0         13.7    0           2.42              18.71       96.3         2.61  19.18       
4 6/11/2021 8:16 4.73        0         13.68   0           2.4               18.41       96.48        3.21  18.77       
5 6/11/2021 8:16 4.52        0         13.14   0           2.47              17.67       96.47        3.78  18.52       
6 6/11/2021 8:17 4.24        0         12.87   0           2.48              17.11       96.4         4.29  18.25  

Anyway, across() works, but it just produces a lot of errors because of the first row.

dt %>% 
  as_tibble() %>% 
  mutate(across(Green.Algae:Temp..Sample, as.numeric))

# A tibble: 6 × 10
  Date.Time      Green.Algae Bluegreen Diatoms Cryptophyta Yellow.substances Total.conc. Transmission Depth Temp..Sample
  <chr>                <dbl>     <dbl>   <dbl>       <dbl>             <dbl>       <dbl>        <dbl> <dbl>        <dbl>
1 date                 NA           NA    NA            NA             NA           NA           NA   NA            NA  
2 6/11/2021 8:16        5.42         0    12.8           0              2.09        18.2         96.2  2.05         19.7
3 6/11/2021 8:16        5.01         0    13.7           0              2.42        18.7         96.3  2.61         19.2
4 6/11/2021 8:16        4.73         0    13.7           0              2.4         18.4         96.5  3.21         18.8
5 6/11/2021 8:16        4.52         0    13.1           0              2.47        17.7         96.5  3.78         18.5
6 6/11/2021 8:17        4.24         0    12.9           0              2.48        17.1         96.4  4.29         18.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.