r - How to deal with spaces in variables when using factor()? -


i'm trying melt , factor csv table looks this:

name        l.i  a.g.a.o.p  e.ngo  s.g.a.o.p    l         29   7          19     5          1   21 g o p   7    5          5      3          0   1 e ngo       19   5          15     3          0   10 s g o p   5    3          3      19         5   18           1    0          0      5          0   3           21   1          10     18         3   12 

with code:

mylevels <- table$name table.m <- melt(table) table.m$name <- factor(table.m$name,levels=mylevels) table.m$variable <- factor(table.m$variable, levels=mylevels) 

the last factoring produces this:

  name      variable value 1 l           <na>    15 2 g o p     <na>     3 3 e ngo         <na>     6 4 s g or p    <na>   -11 5             <na>    -4 6             <na>    -2 7 l           <na>     3 8 g o p     <na>     4 9 e ngo         <na>     1 10 s g o p    <na>    -2 11 academia     <na>    -1 12            <na>    -4 13 l          <na>     6 14 g o p    <na>     1 15 e ngo        <na>    10 16 s g o p    <na>    -8 17            <na>    -4 18            <na>   -10 19 l          <na>   -11 20 g o p    <na>    -2 21 e ngo        <na>    -8 22 s g o p    <na>    15 23            <na>    -2 24            <na>     6 25 l                -4 26 g o p          -1 27 e ngo              -4 28 s g o p          -2 29                   0 30                   0 31 l                -2 32 g o p          -4 33 e ngo             -10 34 s g o p           6 35                   0 36                   8 

i'm guessing factoring didn't space in names variables containing spaces replaced . how deal spaces in variables kind of scenario?

the values of table.m$variable being assigned column names, have had dots substituted spaces (and other characters illegal in variable names). can convert dots spaces via

table.m$variable <- gsub("\\."," ",as.character(table.m$variable)) 

before last line, looks fine. alternatively might try reading data in check.names=false in read.table/read.csv call ...


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -