Hello,
I am having issues with the class and mode of the variables in a dataframe (df_a):
glimpse(df_a)
Rows: 116
Columns: 9
`Provincias y Comunidades Autonomas` <chr> "A Coruña", "Lugo", "Ourense", "Pontevedra", "GALICIA", "P. DE ASTURIAS", …
Superficie Secano (ha)
"1.897", "3.271", "9.413", "326", "14.907", "50", "462", "26.040", "26.040…
`Superficie Regadio (ha)` <chr> "–", "–", "–", "–", "–", "–", "–", "–", "–", "13.374", "5.309", "16.828", …
Superficie Total (ha)
"1.897", "3.271", "9.413", "326", "14.907", "50", "462", "26.040", "26.040…
`Produccion de grano (T)` <chr> "6.317", "8.537", "29.086", "1.187", "45.127", "50", "1.132", "154.938", "…
Paja cosechada (T)
"3.980", "5.849", "16.780", "810", "27.419", "100", "2.060", "113.700", "1…
Año <chr> "2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", "2…
Rendimiento (kg/ha) secano regadio_1
"3.330", "2.610", "3.090", "3.640", "3.027", "1.000", "2.450", "5.950", "5…
$ Rendimiento (kg/ha) secano regadio_2
"–", "–", "–", "–", "–", "–", "–", "–", "–", "5.359", "5.000", "4.680", "4…
After converting the df to numeric values:
df_a2<- as.data.frame(lapply(df_a, as.numeric))
- Also when I do this I loose the first column, that are the names
and checking that values are converted to numbers:
sapply(df_a2,mode)
Provincias.y.Comunidades.Autonomas Superficie.Secano..ha. Superficie.Regadio..ha.
"numeric" "numeric" "numeric"
Superficie.Total..ha. Produccion.de.grano..T. Paja.cosechada..T.
"numeric" "numeric" "numeric"
Año Rendimiento..kg.ha..secano.regadio_1 Rendimiento..kg.ha..secano.regadio_2
"numeric" "numeric" "numeric"
sapply(df_a2, class)
Provincias.y.Comunidades.Autonomas Superficie.Secano..ha. Superficie.Regadio..ha.
"numeric" "numeric" "numeric"
Superficie.Total..ha. Produccion.de.grano..T. Paja.cosechada..T.
"numeric" "numeric" "numeric"
Año Rendimiento..kg.ha..secano.regadio_1 Rendimiento..kg.ha..secano.regadio_2
"numeric" "numeric" "numeric"
When I try to perform any calculation (in this example trying to have the mean of "Superficie.Secano..ha." I get the next error.
df_a2 %>% drop_na() %>% summarize(mean_bl = mean("Superficie.Secano..ha."))
mean_bl
1 NA
Warning message:
There was 1 warning in summarize()
.
In argument:
mean_bl = mean("Superficie.Secano..ha.")
.
Caused by warning in mean.default()
:
! argument is not numeric or logical: returning NA
Does anyone know:
1: why the program is not able to work with the fields once converted to numbers? Is there any issue?
2: Is there a possibility to change the class of all the columns in a dataframe from character to number except one?
thank you in advance