[R] Modifying dataframe with mutate()

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Jul 25 21:42:50 CEST 2020


R is largely a functional language. You do something to an input and end up with an output that has no effect on the input. This is actually a highly desirable feature.

If you want your df variable to reflect changes made then you  need to assign your result back into it.

df <- df %>% mutate(v1 = as.double(v1))

(Note that the data.table package violates this principle and is controversial as a result.)

On July 25, 2020 12:11:24 PM PDT, H <agents using meddatainc.com> wrote:
>In a statement like:
>
>df %>% mutate(v1 = as.double(v1))
>
>I expect the variable v1 in dataframe df to have been converted into a
>double. However, when I do:
>
>str(df)
>
>v1 still shows as int. Do I need to save the modified dataframe after
>mutating a variable?
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list