tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.79k stars 2.12k forks source link

Error in the function 'across()' #7051

Closed Mzhuk7 closed 4 months ago

Mzhuk7 commented 4 months ago

Brief description of the problem: In some of the old courses, I pass using this function from h: h_st <- mutate_each (h,"scale") Rstudio says that this function is obsolete. it suggests to use across. I've checked several sites and created the following function.
h_st <- mutate(across(c("Fertility","Agriculture", "Examination","Education", "Catholic","Infant.Mortality"),scale))

But Rstudio says the following: "Error in across(): ! Must only be used inside data-masking verbs like mutate(), filter(), and group_by()" and I cannot understand the error here, because across is in "()".

Can someone please help to solve this problem?

philibe commented 4 months ago

IMHO you have forgotten the dataset, h in your case:

PS: I'm a simple user (ie not from the tidyverse team).

philibe commented 4 months ago
h<-tibble(
  Fertility=1:5,
  Agriculture=1:5,
  Examination=1:5,
  Education=1:5,
  Catholic=1:5,
  Infant.Mortality=1:5,
  other1=1:5,
  other2=1:5,
)

mutate(h,across(c("Fertility","Agriculture", "Examination","Education", "Catholic","Infant.Mortality"),scale))

results:

# A tibble: 5 × 8
  Fertility[,1] Agriculture[,1] Examination[,1] Education[,1] Catholic[,1] Infant.Mortality[,1] other1 other2
          <dbl>           <dbl>           <dbl>         <dbl>        <dbl>                <dbl>  <int>  <int>
1        -1.26           -1.26           -1.26         -1.26        -1.26                -1.26       1      1
2        -0.632          -0.632          -0.632        -0.632       -0.632               -0.632      2      2
3         0               0               0             0            0                    0          3      3
4         0.632           0.632           0.632         0.632        0.632                0.632      4      4
5         1.26            1.26            1.26          1.26         1.26                 1.26       5      5
DavisVaughan commented 4 months ago

@philibe is right