[R] Extracting part of a factor

KMNanus kmnanus at gmail.com
Sat Mar 5 03:31:32 CET 2016


Boris - 

Boy, do I feel dumb - that’s exactly what I wanted.  I’ve tried this every way I can think of without assigning the result to the original name of the data frame.  I was trying to assign the result to a variable (test$place).

Can u pls explain to me why assigning the result to the new variable was wrong?

BTW, really appreciate your help.
 
Ken
kmnanus at gmail.com
914-450-0816 (tel)
347-730-4813 (fax)



> On Mar 4, 2016, at 9:21 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
> 
> 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