[R] Extracting part of a factor

Boris Steipe boris.steipe at utoronto.ca
Sat Mar 5 03:21:06 CET 2016


LOL you still need to assign it though:


test <- mutate(test, place = factor(substr(test$subject,1,3)))

str(test)
'data.frame':	6 obs. of  7 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1    : int  2 7 9 5 2 1
 $ wk2    : int  3 6 4 7 6 4
 $ wk3    : int  4 5 6 8 3 7
 $ wk4    : int  5 4 1 9 8 4
 $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6


Without assigning the result, the output only gets printed to console. Remember that R is a functional language - a properly written R functio does not change anything, it only returns its result.

:-)


On Mar 4, 2016, at 4:13 PM, KMNanus <kmnanus at gmail.com> wrote:

> If I call mutate this way - mutate(test, place = factor(substr(test$subject,1,3))), I get the same output as above but when I call class(test$place), I get NULL and the variable disappears.



More information about the R-help mailing list