Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another option using mutate_if() from dplyr which allows you to operate on columns for which a predicate returns TRUE

library(dplyr)
df %>% 
  mutate_if(is.factor, funs(as.numeric(as.character(.))))

Note: This method will work for your follow up question as well


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...