dataframe - R change strings into numeric in a data frame -


i have data frame below , generate data frame replacing characters in dataframe numeric according condition.

condition: gd  5 o   4.5 av  3 b   2.5  dataframe: name    peter   paul    mary    chan    jose nokia   b       o       o       gd      gd iphone  av      o       o       o       o moto    av      av      o       o       av htc     b       o       av      av      av  dput: structure(list(name = c("nokia", "iphone", "moto", "htc"), peter =  c("b", "av", "av", "b"), paul = c("o", "o", "av", "o"), mary =  c("o", "o", "o", "av"), chan = c("gd", "o", "o", "av"), jose =  c("gd", "o", "av", "av")), .names =  c("name", "peter", "paul", "mary", "chan", "jose"), class = "data.frame", row.names = c(na, -4l))  desired output: name    peter   paul    mary    chan    jose nokia   2.5     4.5     4.5     5       5 iphone  3       4.5     4.5     4.5     4.5 moto    3       3       4.5     4.5     3 htc     2.5     4.5     3       3       3 

thanks help!!!

one option using match looping through columns

df1[-1] <- lapply(df1[-1], function(x) keyval$v2[match(x, keyval$v1)]) df1 #    name peter paul mary chan jose #1  nokia   2.5  4.5  4.5  5.0  5.0 #2 iphone   3.0  4.5  4.5  4.5  4.5 #3   moto   3.0  3.0  4.5  4.5  3.0 #4    htc   2.5  4.5  3.0  3.0  3.0 

where 'keyval' 'data.frame' 'v1' , 'v2' 'key' , 'value' columns.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -