I have a dataframe in R with a vector of non-sequential numbers (data$SiteID
) that i would like to map to a vector of sequential numbers (data$site
) to the unique values of data$SiteID
. Within each site, I would like to map data$TrtID
to 0
where data$TrtID == 'control'
or to the next sequential integer, for the other unique data$TrtID
's:
data <- data.frame(SiteID = c(1,1,1,9,'108','108','15', '15'),
TrtID = c('N', 'control', 'N', 'control', 'P', 'control', 'N', 'P'))
data$site
should be c(1,1,1,2,3,3,4,4)
.data$trt
should be c(1,0,1,0,1,0,0,1)
.Just treat them as factors:
as.numeric(factor(data$SiteID, levels = unique(data$SiteID)))
[1] 1 1 1 2 3 3 4 4
and for the Trt, since you want a 0-based value, subtract one.
as.numeric(factor(data$TrtID, levels = sort(unique(data$TrtID))))-1
[1] 1 0 1 0 2 0 1 2
Notice that the levels arguments are different - Trt sorts first, which is convinient since control is alphabetically before N or P. If you want a non-standard sorting, you can just explicitly specify the levels in the order you want them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With