R Checking if data table values are same level -


suppose have data table, g, looks this:

g <- read.table(file = textconnection('id answers questions aa 1111111 222222222 aa 2222222 333333333 bb 3333333 444444444 bb 4444444 555555555 bb 5555555 666666666 cc 6666666 777777777'), header = true)  nlevels(g$id) # 3 

for every different id, want create random hexcolor , assign variable, in case there three, given loop:

for(j in 1:nlevels(g$id)) {   rcolor <- paste(sample(0:255,size=3,replace=true),collapse=" ")   assign(paste0("idcolor", j), rcolor) }  idcolor1 # [1] "50 25 197"      # <--corresponds aa idcolor2 # [1] "131 255 58"     # <--corresponds bb idcolor3 # [1] "242 100 72"     # <--corresponds cc 

so there 3 variables each of 3 levels, aa, bb, , cc.

i want output each of rows correct hex color, don't know how check value in g[,1] corresponds idcolor variable. have right trying paste every row variable m.

m <- "" for(i in 1:nrow(g)){   m[i] <- paste0(g[i,1],":",idcolor1) } 

my desired output be:

m # [1] aa:50 25 197 # [2] aa:50 25 197 # [3] bb:131 255 58 # [4] bb:131 255 58 # [5] bb:131 255 58 # [6] cc:242 100 72 

a useful strategy matching values items using merge(), matching creating data frame of matching values. it's simple matter of combining variable using paste like.

g <- read.table(file = textconnection('id answers questions aa 1111111 222222222 aa 2222222 333333333 bb 3333333 444444444 bb 4444444 555555555 bb 5555555 666666666 cc 6666666 777777777'), header = true)  for_merging <- data.frame( id=c("aa","bb","cc"), response = c("50 25 197","131 255 58","242 100 72"))  > merge(g, for_merging)   id answers questions   response 1 aa 1111111 222222222  50 25 197 2 aa 2222222 333333333  50 25 197 3 bb 3333333 444444444 131 255 58 4 bb 4444444 555555555 131 255 58 5 bb 5555555 666666666 131 255 58 6 cc 6666666 777777777 242 100 72 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -