给定条件强制字符串的函数

Function to coerce strings given a condition

我只是想强制转换为数字——即,将 as.numeric 应用到——任何以 1 作为第一个条目(即字符)的列。所以我希望转:

tibble(a = c("1", "2"), b = c("Fred", "Kevin"), c = 1:2)

进入

tibble(a = 1:2, b = c("Fred", "Kevin"), c = 1:2)

您可以使用 dplyr:

library(dplyr)

data %>% 
  mutate(across(where(~ first(.x) == "1" & !is.na(first(.x))), as.numeric)).

returns

# A tibble: 2 x 5
      a b         c     d e    
  <dbl> <chr> <dbl> <dbl> <lgl>
1     1 Fred      1     1 NA    
2     2 Kevin     2     3 NA   

数据

data <- tibble(a = c("1", "2"), 
               b = c("Fred", "Kevin"), 
               c = 1:2, 
               d = c("1", "3"), 
               e = c(NA, NA))

它并没有严格按照您的要求执行,但您可以使用 readr 的 guess_parserparse_guess 函数。有关详细信息,请参阅 https://readr.tidyverse.org/reference/parse_guess.html

在你的情况下你可以这样做:

df %>% mutate(across(everything(),parse_guess))

这将解析所有列。或者仅解析列是否为数字:

parse_guess_numeric <- function (x){
    if (guess_parser(x, guess_integer=FALSE)=="double"){
        as.numeric(x)
    } else {
        x
    }

}

df %>% mutate(across(everything(),parse_guess_numeric))

有很多方法可以解决这个问题:使用 type.converttype_convert from readr:

type.convert(df, as.is = TRUE)
# A tibble: 2 x 3
      a b         c
  <int> <chr> <int>
1     1 Fred      1
2     2 Kevin     2


readr::type_convert(df)

-- Column specification ---------------------------------------------------------------------
cols(
  a = col_double(),
  b = col_character()
)

# A tibble: 2 x 3
      a b         c
  <dbl> <chr> <int>
1     1 Fred      1
2     2 Kevin     2
library(tidyverse)
df <- tibble(a = c("1", "2"), b = c("Fred", "Kevin"), c = 1:2, d = c(NA, NA))
fltr <- names(df)[map_chr(df, guess_parser) == "double"]
mutate(df, across(all_of(fltr), as.numeric))
#> # A tibble: 2 x 4
#>       a b         c d    
#>   <dbl> <chr> <dbl> <lgl>
#> 1     1 Fred      1 NA   
#> 2     2 Kevin     2 NA

reprex package (v2.0.0)

于 2021-07-04 创建