Last active
February 1, 2019 14:17
-
-
Save padpadpadpad/c5b2a8e8d58e1f8e47d8ea1b6e409d55 to your computer and use it in GitHub Desktop.
Random script converting symbols and numbers to just numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load package | |
| library(dplyr) | |
| library(tidyr) | |
| # read in data | |
| d <- readRDS('~/Desktop/subs_GUD') | |
| glimpse(d) | |
| # need to group by tray1,day, site, trayno, type | |
| # the subject column has the correct grouping but the identifier is not numeric - it goes 1-9 then uses symbols (long story). | |
| # these just need to be consecutive numbers to represent the visit number for that tray, for that day at that site etc | |
| # lets have an easy example | |
| x <- c(1,1,1,2,2,2,3,3,3,'a','a','a') | |
| # want these to be 1,2,3,4 etc | |
| nums <- 1:length(unique(x)) | |
| as.character(factor(x, labels = 1:length(unique(x)))) | |
| # works!!! | |
| x <- d$subject | |
| nums <- 1:length(unique(x)) | |
| as.character(factor(x, levels = unique(x), labels = 1:length(unique(x)))) | |
| # method 1 | |
| d <- group_by(d, tray1, day, site, trayno, type) %>% | |
| mutate(subject2 = as.character(factor(subject, levels = unique(x), labels = 1:length(unique(subject))))) %>% | |
| ungroup() | |
| # method 2 | |
| d <- mutate(d, subject3 = group_indices(d, factor(subject, levels = unique(subject)), tray1, day, site, trayno, type)) %>% | |
| ungroup() | |
| # essentially when characters are coerced into factors the levels of the factor are assigned a certain way, and both method 1 and method 2 assign symbols first (i.e. ! = 1, ? = 2, 1 = 3, instead of 1 = 1, 2 = 2 etc) | |
| # so I had to change the levels to be unique(x) for method 1 and method 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment