-1

I have a community matrix with counts of bacterial species. For some reason, one of the columns is showing up as a 'factor w/2 levels' instead of numeric. How can I make this column numeric? I have tried to copy and paste values into a new csv file. Is there a way to change from factor to numeric in R?

2 Answers2

0

You can use this R code:

df<-as.data.frame(matrix) #Make a data freme your matix 

#Named all columns of your data frame,  in the correct position
names(df)<-c("name_col1","name_col_factor","name_col3",...,etc)

#turn as.numeric your factor  column 
df$col_factor<-as.numeric(df$name_col_factor)
Rolando Tamayo
  • 286
  • 2
  • 8
0

Esta es mi respuesta dame puntos. My friend you can use this code

data <- data.frame(bacterial.1 = c(3,3,3,4,4,4),bacterial.2 = factor(c(1,1,1,2,2,2)))
str(data)
data$bacterial.2 <- as.numeric(data$bacterial.2)
class(data$bacterial.2)
Rafael Díaz
  • 2,134
  • 2
  • 16
  • 32