使用tidyverse

时间:2018-06-08 05:10:20

标签: r replace tidyverse data-cleaning mutate

下面是我所拥有的更大尺寸的较小版本,我想根据bandNumber列中的值或基于行位置将reflectanceSfp和reflectanceDT中的值替换为NA。我想特别用tidyverse管道和相关的包来解决这个问题。

 reflectanceSfp wavelength bandNumber reflectanceDT wavelength1
     -0.0113          376       1.00      0.000148         377
     -0.000592        381       2.00      0.00589          382
      0.0158          386       3.00      0.0101           387
      0.0200          391       4.00      0.0110           392
      0.0240          396       5.00      0.0117           397
      0.0265          401       6.00      0.0149           402

所以我有以下不良乐队列表,这些是我想用NA取代的乐队编号:

badBands <- c(1:2,6)

我尝试过这种格式的东西只是为了看它会做什么

m2 <- myData %>%
  mutate(reflectanceSfp = case_when(bandNumber == 1.00 ~ NA ))

但是,最终会希望坏带矢量在管道中 并试图了解modify_at和mutate_at的用法。

我希望结果数据集看起来像

 reflectanceSfp wavelength bandNumber reflectanceDT wavelength1
          NA          376       1.00      0.000148         377
          NA          381       2.00      0.00589          382
      0.0158          386       3.00      0.0101           387
      0.0200          391       4.00      0.0110           392
      0.0240          396       5.00      0.0117           397
          NA          401       6.00      0.0149           402

以下是我的表格的输入版本:

myData <- structure(list(reflectanceSfp = c(-0.011258, -0.000592, 0.015815, 
0.019991, 0.023965, 0.026547), wavelength = c(376.440002, 381.450012, 
386.459991, 391.470001, 396.470001, 401.480011), bandNumber = c(1, 
2, 3, 4, 5, 6), reflectanceDT = c(0.00014819, 0.00589207, 0.01012335, 
0.01101705, 0.01165185, 0.01486412), wavelength1 = c(376.6300049, 
381.6400147, 386.6499939, 391.6600037, 396.6600037, 401.6700134
)), .Names = c("reflectanceSfp", "wavelength", "bandNumber", 
"reflectanceDT", "wavelength1"), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

3 个答案:

答案 0 :(得分:4)

作为&#39; badBands&#39; dig PTR 41.1.168.192.in-addr.arpa使用length代替%in%==也是类型敏感的,因此最好使用正确的case_when ie { {1}}列的{1}}

NA

或者在这里使用NA_real_更容易,我们必须只指定满足逻辑条件且没有类型检查的替换值

double

答案 1 :(得分:1)

myData %>% 
  mutate(reflectanceSfp = ifelse(bandNumber %in% badBands, NA, reflectanceSfp))

# A tibble: 6 x 5
  reflectanceSfp wavelength bandNumber reflectanceDT wavelength1
           <dbl>      <dbl>      <dbl>         <dbl>       <dbl>
1        NA            376.         1.      0.000148        377.
2        NA            381.         2.      0.00589         382.
3         0.0158       386.         3.      0.0101          387.
4         0.0200       391.         4.      0.0110          392.
5         0.0240       396.         5.      0.0117          397.
6        NA            401.         6.      0.0149          402.

答案 2 :(得分:1)

myData%>%
    mutate(reflectanceSfp=`is.na<-`(reflectanceSfp,badBands))
# A tibble: 6 x 5
  reflectanceSfp wavelength bandNumber reflectanceDT wavelength1
           <dbl>      <dbl>      <dbl>         <dbl>       <dbl>
1        NA            376.         1.      0.000148        377.
2        NA            381.         2.      0.00589         382.
3         0.0158       386.         3.      0.0101          387.
4         0.0200       391.         4.      0.0110          392.
5         0.0240       396.         5.      0.0117          397.
6        NA            401.         6.      0.0149          402.