Reassigning database ID subject strings into factors

I need to change strings representing subject IDs into factors or anything more palatable than server database exported strings. The strings look like this:

"0762e6cc-2f79-49ab-9927-e708d7d191a3", "1c24e4a5-10df-48c8-a1e5-98690c25a0ab", etc.

I'd like them to look like the subject IDs we are using that come from a Qualtrics-type questionnaire. Those look like:

0001, 0002, etc.

It's easy enough to recode a factor into the latter, but I can't figure out to how to replace the strings with factors.

Please and thank you!

I expect there is a more elegant way to do this but here is what I would do.

DF <- data.frame(ID = c("0762e6cc-2f79-49ab-9927-e708d7d191a3", "1c24e4a5-10df-48c8-a1e5-98690c25a0ab"),
                 stringsAsFactors = FALSE)
summary(DF)
#>       ID           
#>  Length:2          
#>  Class :character  
#>  Mode  :character
DF$ID <- factor(DF$ID)
DF$ID2 <- as.numeric(DF$ID)
DF
#>                                     ID ID2
#> 1 0762e6cc-2f79-49ab-9927-e708d7d191a3   1
#> 2 1c24e4a5-10df-48c8-a1e5-98690c25a0ab   2

Created on 2020-04-29 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Thank you!

I modified the first bit to include the full column of IDs and it still worked well.

It looked like this:

DF <- data.frame(ID = c(Level1$subject_id),
stringsAsFactors = FALSE)

Thanks again!