I have 50 data frames (different name each) with 10 (same name) columns of climate data. The first 5 columns although they are numbers, their class is "character". The rest 4 columns are already in the correct class (numeric) and the last one (named 'wind dir') is in character class so no change is needed.
I tried two ways to convert the class of those 5 columns in all 50 data frames, but nothing worked.
1st way) Firstly I've created a vector with the names of those 50 data frames and I named it onomata.
Secondly I've created a vector col_numbers2 <- c(1:5)
with the number of columns I would like to convert.
Then I wrote the following code:
for(i in onomata){
i[col_numbers2] <- sapply(i[col_numbers2], as.numeric)
}
Checking the class of those first five columns I saw that nothing changed. (No error report after executing the code)
2nd way) Then I tried to use the dplyr package with a for loop and the code is as follows:
for(i in onomata){
i <- i %>%
mutate_at(vars(-`wind_dir`),as.numeric)
In this case, I excluded the character column, and I applied the mutate function to the whole data frame, but I received an error message :
Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "character"
What do you think I am doing wrong ?
Thank you
Original data table (what I get when I use read.table() for each txt file:
date | Time | Tdry | Humidity | Wind_velocity | Wind_direction | Wind_gust |
---|---|---|---|---|---|---|
02/01/15 | 02:00 | 2.4 | 77.0 | 6.4 | WNW | 20.9 |
02/01/15 | 03:00 | 2.3 | 77.0 | 11.3 | NW | 30.6 |
02/01/15 | 04:00 | 2.3 | 77.0 | 9.7 | NW | 20.9 |
02/01/15 | 05:00 | 2.3 | 77.0 | 11.3 | NW | 30.6 |
02/01/15 | 06:00 | 2.3 | 78.0 | 9.7 | NW | 19.3 |
02/01/15 | 07:00 | 2.2 | 79.0 | 12.9 | NNW | 35.4 |
02/01/15 | 08:00 | 2.4 | 79.0 | 8.0 | NW | 14.5 |
02/01/15 | 09:00 | 2.6 | 79.0 | 8.0 | WNW | 20.9 |
Data after I split data in columns 1 and 2 (date, time):
day | month | year | Hour | Minutes | Tdry | Humidity | Wind_velocity | Wind_direction | Wind_gust |
---|---|---|---|---|---|---|---|---|---|
02 | 01 | 15 | 02 | 00 | 2.4 | 77.0 | 6.4 | WNW | 20.9 |
02 | 01 | 15 | 03 | 00 | 2.3 | 77.0 | 11.3 | NW | 30.6 |
02 | 01 | 15 | 04 | 00 | 2.3 | 77.0 | 9.7 | NW | 20.9 |
02 | 01 | 15 | 05 | 00 | 2.3 | 77.0 | 11.3 | NW | 30.6 |
02 | 01 | 15 | 06 | 00 | 2.3 | 78.0 | 9.7 | NW | 19.3 |
02 | 01 | 15 | 07 | 00 | 2.2 | 79.0 | 12.9 | NNW | 35.4 |
02 | 01 | 15 | 08 | 00 | 2.4 | 79.0 | 8.0 | NW | 14.5 |
02 | 01 | 15 | 09 | 00 | 2.6 | 79.0 | 8.0 | WNW | 20.9 |