Monday, 20 March 2017

r - How to convert all column data type to numeric and character dynamically?



I convert my columns data type manually:



data[,'particles'] <- as.numeric(as.character(data[,'particles']))



This not ideal as the data may evolve and I won't be sure what species coming, for instance they could be - "nox", "no2", "co", "so2", "pm10" and more in the future.



Is there anyway to convert them automatically?



My current dataset:



structure(list(particles = structure(c(1L, 3L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 5L, 6L, 5L, 3L), .Label = c("1", "11", "1.1",
"2", "2.1", "3.1"), class = "factor"), humidity = structure(c(4L,

7L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 1L, 1L, 1L,
5L, NA, NA, NA, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0.1",
"1", "1.1", "1.3", "21", "2.1", "3"), class = "factor"), timestamp = c(1468833354929,
1468833365186, 1468833378458, 1468833538213, 1468833538416, 1468833538613,
1468833538810, 1468833538986, 1468833539172, 1468833539358, 1468833539539,
1468833554592, 1468833559059, 1468833562357, 1468833566225, 1468833573486,
1468840019118, 1468840024950, 1469029568849, 1469029584243, 1469029590530,
1469029622391, 1469029623598, 1469245154003, 1469245156533, 1469245156815,
1469245157123, 1469245162358, 1469245165911, 1469245170178, 1469245173788
), date = structure(c(1468833354.929, 1468833365.186, 1468833378.458,

1468833538.213, 1468833538.416, 1468833538.613, 1468833538.81,
1468833538.986, 1468833539.172, 1468833539.358, 1468833539.539,
1468833554.592, 1468833559.059, 1468833562.357, 1468833566.225,
1468833573.486, 1468840019.118, 1468840024.95, 1469029568.849,
1469029584.243, 1469029590.53, 1469029622.391, 1469029623.598,
1469245154.003, 1469245156.533, 1469245156.815, 1469245157.123,
1469245162.358, 1469245165.911, 1469245170.178, 1469245173.788
), class = c("POSIXct", "POSIXt"), tzone = "Asia/Singapore")), .Names = c("particles",
"humidity", "timestamp", "date"), row.names = c(NA, -31L), class = "data.frame")



It has particles, humidity, timestamp, date.


Answer



If you don't know which columns need to be converted beforehand, you can extract that info from your dataframe as follows:



vec <- sapply(dat, is.factor)


which gives:




> vec
particles humidity timestamp date
TRUE TRUE FALSE FALSE


You can then use this vector to do the conversion on the subset with lapply:



# notation option one:
dat[, vec] <- lapply(dat[, vec], function(x) as.numeric(as.character(x)))
# notation option two:

dat[vec] <- lapply(dat[vec], function(x) as.numeric(as.character(x)))


If you want to detect both factor and character columns, you can use:



sapply(dat, function(x) is.factor(x)|is.character(x))

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...