楼主: 天涯印象
9496 10

[实际应用] 写一个判断语句,自动对数据框赋值 [推广有奖]

11
天涯印象 学生认证  发表于 2017-9-7 10:38:23
  1. test <- data.frame(X1=c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE,FALSE,FALSE),
  2.                     X2=c(001, NA, NA, 002, NA, 003, NA, NA, NA))
  3. #step1
  4. index <- which(!is.na(test[,2]))
  5. #step2
  6. delta <- diff(c(index,length(test$X2)+1))
  7. #final step
  8. result <- rep(test[,2][index],delta)
  9. test3<- cbind.data.frame(test$X1, result)
  10. write.csv(test3,file = "result.csv")
复制代码
最后的输出结果:
QQ截图20170907103548.jpg

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 09:29