检查所有值是否在多个列上都是数字,并将它们转换为数字

时间:2018-10-01 13:54:30

标签: r dataframe dplyr data.table tidyr

我有一个数据框,所有列都是这样的字符。

ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
            "CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
          "Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
                 "Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
              "Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
          "ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
             "CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")

df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)

我正在尝试检查这些字符向量中的数字,然后将那些具有数字值的字符转换为数字。我在R中使用“ varhandle”包

library(varhandle)

if(all(check.numeric(df$Machine, na.rm=TRUE))){
  # convert the vector to numeric
  df$Machine <- as.numeric(df$Machine)
}

这行得通,但是效率不高,因为我必须像上面一样手动输入列名。如何在循环中更有效地执行此操作,或者如何在多个列上使用向量化?我的实际数据集大约有350列。有人可以指出我正确的方向吗?

5 个答案:

答案 0 :(得分:5)

我们可以使用parse_guess包中的readr函数,该函数基本上试图猜测列的类型。

library(readr)
library(dplyr)

df1 <- df %>% mutate_all(parse_guess)


str(df1)
#'data.frame':  16 obs. of  11 variables:
# $ ID         : chr  "A" "A" "A" "A" ...
# $ ToolID     : chr  "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step       : chr  "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr  "Length" "Breadth" "Width" "Height" ...
# $ Passfail   : chr  "Pass" "Pass" "Fail" "Fail" ...
# $ Points     : int  7 5 3 4 0 0 0 0 17 15 ...
# $ Average    : num  7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma      : num  2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool       : chr  "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose       : num  NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine    : chr  "CO2" "CO6" "CO3" "CO6" ...

答案 1 :(得分:2)

我们可以在base R

中完成此操作
df[] <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame':  16 obs. of  11 variables:
# $ ID         : chr  "A" "A" "A" "A" ...
# $ ToolID     : chr  "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step       : chr  "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr  "Length" "Breadth" "Width" "Height" ...
# $ Passfail   : chr  "Pass" "Pass" "Fail" "Fail" ...
# $ Points     : int  7 5 3 4 0 0 0 0 17 15 ...
# $ Average    : num  7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma      : num  2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool       : chr  "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose       : num  NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine    : chr  "CO2" "CO6" "CO3" "CO6" ...

答案 2 :(得分:1)

使用varhandle和tidyverse:

df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)

答案 3 :(得分:0)

我认为最简单的解决方案是使用all.is.numeric中的Hmisc。这是简单的示例:

Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1]  3  2  1 NA

然后,您可以使用dplyr中的mutate_all来完成data.frame的所有工作:

library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
            "CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
          "Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
                 "Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
              "Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
          "ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
             "CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")

df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)

dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))

## check classes
sapply(dt2, class)

##         ID      ToolID        Step Measurement    Passfail      Points 
## "character" "character" "character" "character" "character"   "numeric" 
##    Average       Sigma        Tool        Dose     Machine 
##  "numeric"   "numeric" "character"   "numeric" "character" 

答案 4 :(得分:0)

另一种解决方法是从hablar包中重新键入:

implementation 'androidx.appcompat:appcompat:1.0.0'

给出:

library(hablar)

df %>% retype()