R: tidyr
Revision as of 13:59, 23 April 2015 by Rafahsolis (talk | contribs) (Created page with "== tidyr Package == http://vita.had.co.nz/papers/tidy-data.pdf <source lang="rsplus"> # gather() # Having a dataset "students" with colums "grade", "male", "female" gather(stu...")
tidyr Package
http://vita.had.co.nz/papers/tidy-data.pdf
# gather()
# Having a dataset "students" with colums "grade", "male", "female"
gather(students, sex, count, -grade)
# Will return a dataset with the colums grade, sex, count
If we have a dataset with multiple variables stored in one column, for example:
grade, male_1, female_1, male_2, female_2, are colums storing grades for two diferent classes.
res <- gather(students2, sex_class, count, -grade)
separate(res, col=sex_class, into = c("sex", "class"))
# or using pipelines:
students2 %>%
gather( sex_class, count, -grade) %>%
separate( col=sex_class, into = c("sex", "class")) %>%
print
If there are variables stored in both rows ad colums:
> students3
name test class1 class2 class3 class4 class5
1 Sally midterm A <NA> B <NA> <NA>
2 Sally final C <NA> C <NA> <NA>