我还要查找缺失值之前的值是否小于,等于或大于缺失值之后的值.要使用之前的相同示例,请执行以下操作: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))行13、14和16在缺失值之间具有不丢失的值.这次的输出应为:第13、14和16行为相同",较大"和相同",其他行为"N/A".解决方案一种简单的方法是拆分,转换为数字,获取最后2个值并与ifelse语句进行比较,即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" 注意我将先前的答案作为给定的(do.call(paste, df)[c(13, 14, 16)])一种更通用的方法(如罗纳克(Ronak)所述,在某些情况下,后两位数字可能会失败),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"To continue on a previous topic:Finding non-missing values between missing valuesI 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. 解决方案 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"NOTEI 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" 这篇关于介于两者之间的缺失值的非缺失值的级数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 04:05