The problem is that R
understands that dadosarrumados[, c(4, 5)]
is a list:
is.list(dadosarrumados[, c(4, 5)])
[1] TRUE
One way to solve this problem is to undo the list and then convert it to numeric:
as.numeric(unlist(dadosarrumados[, c(4, 5)]))
[1] 5068.075 348.574 42.420 18.042 73.231 5.000 5.200 6.000
[9] 6.300 4.000
But notice that we got out of one problem and fell into another: we lost the formatting that was in two columns. The unlist
function has transformed the dataset into a vector. We could transform this vector into a data frame, but I prefer another approach.
Use the apply
function. It is used to apply other functions in columns or rows of data frames. For example, when rotating
apply(dadosarrumados[, c(4, 5)], 2, as.numeric)
Quantidade Porcentagem
[1,] 5068.075 5.0
[2,] 348.574 5.2
[3,] 42.420 6.0
[4,] 18.042 6.3
[5,] 73.231 4.0
I'm told to R
apply ( apply
) to the as.numeric
function in the columns (number 2
) of the data frame dadosarrumados[, c(4, 5)]
. If I had used 1
instead of 2
in the second argument of apply
, the as.numeric
function would have been applied on the lines and then we would not have the desired result.
One way to get the full frame data, with the columns converted to numeric, is to do this:
bind_cols(dadosarrumados[, 1:3],
as_data_frame(apply(dadosarrumados[, c(4, 5)], 2, as.numeric)))
# A tibble: 5 x 5
Região Total 'Anos de estudo' Quantidade Porcentagem
<chr> <dbl> <chr> <dbl> <dbl>
1 Brasil 102083 menor que 4 anos 5068. 5
2 Norte 6715 menor que 4 anos 349. 5.2
3 Rondônia 711 menor que 4 anos 42.4 6
4 Acre 285 menor que 4 anos 18.0 6.3
5 Amazonas 1597 menor que 4 anos 73.2 4
I'm using the bind_cols
function to join two data frames: the original, from columns 1 to 3, and the resultant from the conversion we made above.