Convert data.frame object into numeric

0

I have a data.frame file and need to convert to numeric values to do column summation. I used the "dicpes < -as.numeric (dicpes)" code to try to convert and the following message appeared: "Error: (list) object can not be coerced to type 'double'.

Looking at the response from another topic, I decided to separate the columns of the object and convert them individually into numeric values using the following code:

inicio<- dicpes[,1]
inicio<-as.numeric(as.character(inicio)) # transformando em números
tamanho<- dicpes[,2]
tamanho<-as.numeric(as.character(tamanho)) # transformando em números
variavel<- dicpes[,3] # coluna em formato texto
variavel<- as.character(variavel) # manter em formato texto
dicpes<- cbind(inicio, tamanho, variavel)

However, when the last command, the first two columns (start and size) are read again with text too and I can not sum. How to work around this problem?

    
asked by anonymous 02.03.2016 / 03:55

1 answer

0

@Julio Cesar, you can not convert a data.frame numerically all columns at once. For this there is some solution.

Example:

df <- data.frame("col_1" = as.character(seq(1,10)), "col_2" = as.character(seq(11,20)))

1. Separate transformations individually by columns

df$col_1 <- as.numeric(df$col_1)
df$col_2 <- as.numeric(df$col_2)

2. Use lapply

df[, c(1,2)] <- lapply(df[c(1,2)], as.numeric)

With lapply, I can select only the columns I want to be transformed.

09.10.2016 / 01:19