What is an effective way to match unique vector values ​​with consecutive integers?

I have a dataframe in R with a vector of non-second numbers ( data$SiteID) that I would like to map to a vector of sequential numbers ( data$site) with unique values data$SiteID. Inside each site, I would like to match data$TrtIDwith 0, where data$TrtID == 'control'or to the next consecutive number, for other unique ones data$TrtID:

data <- data.frame(SiteID = c(1,1,1,9,'108','108','15', '15'), 
                   TrtID = c('N', 'control', 'N', 'control', 'P', 'control', 'N', 'P'))
  • data$siteshould be c(1,1,1,2,3,3,4,4).
  • data$trtshould be c(1,0,1,0,1,0,0,1).
+3
source share
2 answers

Use conversion factors to integers:

transform(data, site=as.integer(SiteID), trt=as.integer(TrtID))

If ordering is important, you can give specific orders to the levels:

transform(data,
  site = as.integer(factor(SiteID, unique(SiteID))),
  trt  = as.integer(factor(TrtID, unique(c('control', as.character(TrtID))))) - 1L)

Changed version of trt-factor grouping by site:

transform(data,
  site = as.integer(factor(site_id, unique(site_id))),
  trt  = unsplit(tapply(trt_id, site_id, function(x)
         as.integer(factor(x))), site_id) - 1L)
+4

:

as.numeric(factor(data$SiteID, levels = unique(data$SiteID)))
[1] 1 1 1 2 3 3 4 4

Trt, , 0, .

as.numeric(factor(data$TrtID, levels = sort(unique(data$TrtID))))-1
[1] 1 0 1 0 2 0 1 2

, . Trt, , N P. , , .

+4

Source: https://habr.com/ru/post/1765712/


All Articles