transform - R stats, transforming parts of a data matrix -
i new both stackoverflow , r stats, please bear me. have lot of experience coding in sas, trying learn r. commonly use sas , r transform large datasets, have species study site matrix follows:
species status role site1 site2 site3...site25 a_a s p 0 0 0 1 a_b x 1 25 0 0 b_a s p 0 2 1 1 b_b s x 0 1 0 0 ...
i transform table , create 2 new variables called "site" , "count" based on site variable names , count data within each site:
species status role site count a_a s p site1 0 a_a s p site2 0 a_a s p site3 0 a_a s p site25 1 a_b x site1 1 a_b x site2 25 a_b x site3 0 a_b x site25 0 ... b_b s x site25 0
i think may beyond simple t() function, , have looked packages reshape , reshape2, kind of lost how proceed. have had situation , lend hand coding? thanks, jimh
you can use dplyr/tidyr this:
install.packages(c("tidyr", "dplyr"), dependencies = true) library(dplyr) library(tidyr) df %>% gather(site, count, grep('site', names(df))) %>% arrange(species)
Comments
Post a Comment