How convert columns in double format usign `for` function?

All my columns of years are in character format, but i want convert its on Double format usign the "for" function.
For example, i writed the next command:

for (i in 1999:2020) {
gdp$i <- as.double(gdp$i)
}

All my columns of 1999 to 2020 have values form many countrys gdp, but i need work wihts tihs data on numeric format. I need converting all columns in double format with only one command. Helpme

It will be much easier to help you if you show what your data look like. Please post the output of

dput(head(gdp))

Before you paste the output into your post, type a line with three back ticks, ```, and nothing else on that line. Put a similar line after the output.

Its is the structure of the data

structure(list(Country = c("Antigua and Barbuda", "Argentina", 
"Aruba", "The Bahamas", "Barbados", "Belize"), `1999` = c(1.205, 
-4.429, 0.003, 5.423, -0.037, 6.74), `2000` = c(4.484, -1.828, 
6.487, 2.503, 4.095, 9.972), `2001` = c("-6.548", "-5.367", "-4.2789999999999999", 
"1.109", "-2.67", "1.952"), `2002` = c("-5.3999999999999999E-2", 
"-11.747999999999999", "-3.5190000000000001", "1.2250000000000001", 
"0.497", "1.982"), `2003` = c("4.9260000000000002", "7.9370000000000003", 
"0.82199999999999995", "-2.6469999999999998", "1.879", "5.9459999999999997"
), `2004` = c("4.6079999999999997", "7.8959999999999999", "5.6289999999999996", 
"-0.49099999999999999", "1.097", "1.349"), `2005` = c("5.2759999999999998", 
"7.819", "-1.367", "2.0289999999999999", "3.617", "-1.81"), `2006` = c("11.525", 
"6.9980000000000002", "-0.749", "1.2729999999999999", "5.2729999999999997", 
"1.518"), `2007` = c("8.093", "7.9390000000000001", "1.276", 
"0.23599999999999999", "1.7909999999999999", "-2.7839999999999998"
), `2008` = c("-1.097", "3.0379999999999998", "-0.60899999999999999", 
"-3.4620000000000002", "0.27300000000000002", "8.4000000000000005E-2"
), `2009` = c("-13.045999999999999", "-6.83", "-11.933999999999999", 
"-5.2649999999999997", "-5.468", "-1.4999999999999999E-2"), `2010` = c("-8.5609999999999999", 
"8.36", "-3.5819999999999999", "0.41699999999999998", "-2.6720000000000002", 
"2.7650000000000001"), `2011` = c("-2.972", "4.7889999999999997", 
"2.5630000000000002", "-0.498", "-1.038", "-0.71699999999999997"
), `2012` = c("1.8069999999999999", "-2.145", "-3.0449999999999999", 
"1.948", "-0.79800000000000004", "-0.16700000000000001"), `2013` = c("-2.1059999999999999", 
"1.266", "2.3940000000000001", "-4.024", "-1.734", "-1.288"), 
    `2014` = c(2.225, -3.579, -0.444, -0.376, -0.435, 1.542), 
    `2015` = c(2.252, 1.63, -1.726, -0.513, 2.147, 0.221), `2016` = c(3.901, 
    -3.11, -0.543, -0.663, 2.205, -2.473), `2017` = c(1.584, 
    1.609, 1.884, -1.038, 0.223, -0.733), `2018` = c(5.763, -3.467, 
    0.64, 0.444, -0.835, -0.528), `2019` = c(3.696, -3.132, -0.045, 
    0.677, -0.351, -2.801), `2020` = c("-11.321", "-6.6529999999999996", 
    "-14.071999999999999", "-9.3379999999999992", "-7.8339999999999996", 
    "-13.722")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

This should do what you need without using an explicit loop. I named your data frame DF.

library(dplyr)
DF <- mutate_at(.tbl = DF, .vars = 2:23, .funs = as.numeric)
2 Likes

Thanks so much.
I am indebted to you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.