0

I am encountering a problem when I run the following code to try to convert several columns of data from character to factor:

library(readxl)
library(dplyr)

"LR_df1"<-read_excel("Correct file location.xlsx",sheet="Correct sheet")


LR_df1 %>%
  as_tibble()%>%
  mutate(across(c(SiteCode, S01UsualResidence), as.factor))

The initial output looks good, and it appears that the specified columns have been converted:

    # A tibble: 1,075 x 238
       A CaseId SiteCode S01Gender S01UsualResidence
   <dbl>  <dbl> <fct>    <chr>     <fct>

But then when I run:

summary(LR_df1$SiteCode)

I get:

> summary(LR_df1$SiteCode) Length Class Mode 1075 character character

So it appears that the variable hasn't been converted. I'd be very grateful to anyone who can show me what I'm doing wrong.

donm79
  • 31
  • 1
  • 3

1 Answers1

1

mutate(), like all other functions from dplyr, doesn't permanently change the dataframe it works on. On its own, it will output a changed dataframe, but the original dataframe stays intact.

If you want it to permanently change the dataframe, you need to reassign:

LR_df1 <- LR_df1 |> mutate(...)
Andrea M
  • 2,314
  • 1
  • 9
  • 27