简体   繁体   中英

how to keep values when converting character to factor in R

So I am working with this matrix (see below) on R where you have the individuals and the the number of times they fought on Left,Right and total fight. I would like to do ANOVA to see the difference in number of fights per individuals. However I cannot use the column with the names so I need to add it and that's when I have a problem:

          Left Right Total
DarkMale    0     1     1   
Melman      5     2     7     
Polp        0    12    12       
Sun        10     1    11        
Kevin       0    11    11      
McFly       0    30    30      
Lovely     36     0    36     
Aquarius    0    30    30    
Kenny       0    23    23       
Lethabo    16     0    16     
Charlie     0     3     3    


Indv=rbind("DarkMale","Melman","Polp","Sun","Kevin","McFly","Lovely","Aquarius","Kenny","Lethabo","Charlie")
tab=cbind(tab,Total,Indv)
colnames(tab)=c("Left","Right","Total","Individuals")

I did this but then it converters the rest of table in Character which I cannot use either. I have tried testtab=as.data.frame(tab,stringsAsFactors=FALSE) which got rid of the "" in the table but still keeps all values in character. How can I convert the table by keeping these values (see below) but with it being integer or factor that I could use for anova?

           Left Right Total Individuals
DarkMale    0     1     1    DarkMale
Melman      5     2     7      Melman
Polp        0    12    12        Polp
Sun        10     1    11         Sun
Kevin       0    11    11       Kevin
McFly       0    30    30       McFly
Lovely     36     0    36      Lovely
Aquarius    0    30    30    Aquarius
Kenny       0    23    23       Kenny
Lethabo    16     0    16     Lethabo
Charlie     0     3     3     Charlie

Cheers

We need to first convert to data.frame and then create a column from the row names

d1 <- transform(as.data.frame(m1), Individuals = row.names(m1))

Using cbind on a matrix with a character element/elements convert the whole matrix to character as matrix can hold only a single class. Afterwards, if we convert to data.frame , the class remains as such or change to factor depending on whether stringsAsFactors is FALSE/TRUE .

Here is another way to do it. I generated a matrix to start with what you are starting with, then transformed it into a dataframe . For more compact solution use transform as mentioned in akrun solution.

tab <- matrix(data =c(1:33) , nrow = 11, ncol = 3)
df <- as.data.frame(tab)

Indv <- c("DarkMale","Melman","Polp","Sun","Kevin","McFly","Lovely","Aquarius","Kenny","Lethabo","Charlie")
colnames <- c("Left","Right","Total","Individuals")

df[4] <- Indv

rownames(df) <- Indv
colnames(df) <- colnames
# 
#           Left Right Total Individuals
# DarkMale    1    12    23    DarkMale
# Melman      2    13    24      Melman
# Polp        3    14    25        Polp
# Sun         4    15    26         Sun
# Kevin       5    16    27       Kevin
# McFly       6    17    28       McFly
# Lovely      7    18    29      Lovely
# Aquarius    8    19    30    Aquarius
# Kenny       9    20    31       Kenny
# Lethabo    10    21    32     Lethabo
# Charlie    11    22    33     Charlie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM