Progression of non-missing values that have missing values in-between

我是研究僧i 提交于 2019-12-11 04:19:53

问题


To continue on a previous topic: Finding non-missing values between missing values

I would like to also find whether the value before the missing value is smaller, equal to or larger than the one after the missing.

To use the same example from before:

df = structure(list(FirstYStage = c(NA, 3.2, 3.1, NA, NA, 2, 1, 3.2, 
3.1, 1, 2, 5, 2, NA, NA, NA, NA, 2, 3.1, 1), SecondYStage = c(NA, 
3.1, 3.1, NA, NA, 2, 1, 4, 3.1, 1, NA, 5, 3.1, 3.2, 2, 3.1, NA, 
2, 3.1, 1), ThirdYStage = c(NA, NA, 3.1, NA, NA, 3.2, 1, 4, NA, 
1, NA, NA, 3.2, NA, 2, 3.2, NA, NA, 2, 1), FourthYStage = c(NA, 
NA, 3.1, NA, NA, NA, 1, 4, NA, 1, NA, NA, NA, 4, 2, NA, NA, NA, 
2, 1), FifthYStage = c(NA, NA, 2, NA, NA, NA, 1, 5, NA, NA, NA, 
NA, 3.2, NA, 2, 3.2, NA, NA, 2, 1)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -20L))

rows 13, 14 and 16 having non-missing in between missing values. The output this time should be: "same", "larger" and "same" for rows 13, 14, and 16, and say "N/A" for the other rows.


回答1:


A straight forward approach would be to split, convert to numeric, take the last 2 values and compare with an ifelse statement, i.e.

sapply(strsplit(do.call(paste, df)[c(13, 14, 16)], 'NA| '), function(i){
                                  v1 <- as.numeric(tail(i[i != ''], 2)); 
                                  ifelse(v1[1] > v1[2], 'greater', 
                                           ifelse(v1[1] == v1[2], 'same', 'smaller'))
                                   })

#[1] "same"    "smaller" "same"

NOTE

I took previous answer as a given (do.call(paste, df)[c(13, 14, 16)])

A more generic approach (as noted by Ronak, last 2 digits will fail in some cases) would be,

sapply(strsplit(gsub("([[:digit:]])+\\s+[NA]+\\s+([[:digit:]])", '\\1_\\2', 
                   do.call(paste, df)[c(13, 14, 16)]), ' '), function(i) { 
                                             v1 <- i[grepl('_', i)]; 
                                             v2 <- strsplit(v1, '_')[[1]]; 
                                            ifelse(v2[1] > v2[2], 'greater', 
                                               ifelse(v2[1] == v2[2], 'same', 'smaller')) })

#[1] "same"    "smaller" "same" 


来源:https://stackoverflow.com/questions/56491759/progression-of-non-missing-values-that-have-missing-values-in-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!