data <- c("2018", "5.5", "2016", "8.4", "2017", "6.6", "2018", "2017", "5.5",
"2009", "7.9")
问题在于,虽然所有年份都存在,但某些分数却丢失了:
matrix(data, ncol = 2, byrow = T)
[,1] [,2]
[1,] "2018" "5.5"
[2,] "2016" "8.4"
[3,] "2017" "6.6"
[4,] "2018" "2017"
[5,] "5.5" "2009"
[6,] "7.9" "2018"
这样,当值对移动时,我无法通过将其转换为矩阵或数据框来构造数据。
有没有一种方法可以检测何时发生不匹配,即。一年之后是另一年,并在两个值之间插入一个NA?
答案 0 :(得分:0)
当然,这是一种非常紧凑的方法:
idx <- which(nchar(data) == 4)
cbind(Year = data[idx], Score = ifelse(nchar(data[idx + 1]) == 3, data[idx + 1], NA))
# Year Score
# [1,] "2018" "5.5"
# [2,] "2016" "8.4"
# [3,] "2017" "6.6"
# [4,] "2018" NA
# [5,] "2017" "5.5"
# [6,] "2009" "7.9"
其中使用nchar
的位置和长度信息是关键。