如何将某些列仅转换为数字?

时间:2016-08-01 09:58:20

标签: r dataframe

如何仅将数据框中的某些列转换为数字?

例如,我有这个数据框:

structure(list(airport = c("EGLL", "EGLL"), xdate = c("2016-07-28", 
"2016-07-31"), ws = c("6", "5"), wd = c("237", "299"), humidity = c("68", 
"55")), .Names = c("airport", "xdate", "ws", "wd", "humidity"
), row.names = 1:2, class = "data.frame")

我只想将wswdhumidity转换为数字, airport and xdate

如果我这样做:

columns <- sapply(weatherDF, is.character)
weatherDF[, columns] <- lapply(weatherDF[, columns, drop = FALSE], function(x) as.numeric(as.character(x)))

我正在将airportxdate转换为数字,然后我收到此警告:

Warning messages:
1: In FUN(X[[i]], ...) : NAs introduced by coercion
2: In FUN(X[[i]], ...) : NAs introduced by coercion

现在我的数据框已经变成:

structure(list(airport = c(NA_real_, NA_real_), xdate = c(NA_real_, 
NA_real_), ws = c(6, 5), wd = c(237, 299), humidity = c(68, 55
)), .Names = c("airport", "xdate", "ws", "wd", "humidity"), row.names = 1:2, class = "data.frame")

我有什么想法可以正确转换它们吗?

4 个答案:

答案 0 :(得分:4)

1)您的所有columns都是character  columns <- sapply(weatherDF, is.character)

airport    xdate       ws       wd humidity 
    TRUE     TRUE     TRUE     TRUE     TRUE

2)为什么不简单?

weatherDF[, 3:ncol(weatherDF)] <- lapply(3:ncol(weatherDF), function(x) as.numeric(weatherDF[[x]]))

columns <-c("ws", "wd", "humidity")
weatherDF[, columns] <- lapply(columns, function(x) as.numeric(weatherDF[[x]]))

如果您不知道哪些列是数字,您可以尝试使用tryCatch找到它 像

weatherDF[,1:ncol(weatherDF)]=lapply(1:ncol(weatherDF),function(x) {
  tryCatch({
    as.numeric(weatherDF[[x]])
    },warning = function(w) {
    weatherDF[[x]]}
        )} )

答案 1 :(得分:4)

num.cols <- c('ws','wd','humidity')
weatherDF[num.cols] <- sapply(weatherDF[num.cols], as.numeric)

答案 2 :(得分:2)

使用dplyr

library(dplyr)
df %>% 
  mutate_at(vars(ws, wd, humidity), as.numeric)

# A tibble: 2 x 5
airport xdate         ws    wd humidity
  <chr>   <chr>      <dbl> <dbl>    <dbl>
1 EGLL    2016-07-28    6.  237.      68.
2 EGLL    2016-07-31    5.  299.      55.

答案 3 :(得分:1)

Hmisc包中的all.is.numeric函数可以很好地确定是否可以将给定列强制转换为数字。

使用它,你可以这样做:

numeric_cols <- sapply(weatherDF, Hmisc::all.is.numeric)

if (sum(numeric_cols) > 1)  {
    weatherDF[,numeric_cols] <- data.matrix(weatherDF[,numeric_cols])
} else {
    weatherDF[,numeric_cols] <- as.numeric(weatherDF[,numeric_cols])
}