如何循环遍历行,然后在每行循环遍历列直到找到NA,然后提取前一列的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何循环遍历行,然后在每行循环遍历列直到找到NA,然后提取前一列的值相关的知识,希望对你有一定的参考价值。
这是我的数据集(我用R分析):
mydf <- structure(list(`1` = c(265L, 303L, 200L, 432L, 248L, 344L, 247L,
328L, 543L, 296L, 312L, 296L, 311L, 240L, 345L, 280L, 544L, 200L,
329L, 159L, 399L, 303L, 167L, 320L, 223L, 200L, 320L, 431L, 384L,
223L, 450L, 439L, 568L, 453L, 541L, 466L, 407L, 447L, 472L, 495L,
555L, 675L, 350L, 1032L, 435L, 434L, 345L, 337L, 478L, 477L,
450L, 447L, 470L, 400L, 486L, 422L, 462L, 390L, 660L, 406L, 194L,
391L, 385L, 309L, 282L, 345L, 312L, 266L, 438L, 400L, 442L, 282L,
562L, 376L, 477L, 463L, 345L, 396L, 346L, 263L, 253L, 233L, 267L,
279L, 238L, 407L, 369L, 273L, 304L, 260L), `2` = c(200L, 264L,
176L, 336L, 208L, 256L, 232L, 304L, 488L, 240L, 280L, 231L, 177L,
272L, 183L, 184L, 456L, 200L, 264L, 169L, 313L, 208L, 264L, 216L,
169L, 184L, 224L, 304L, 272L, 225L, 323L, 272L, 300L, 260L, 401L,
724L, 427L, 255L, 298L, 299L, 329L, 275L, 272L, 898L, 396L, 257L,
423L, 386L, 330L, 441L, 453L, 205L, 556L, 304L, 297L, 499L, 303L,
226L, 423L, 300L, 255L, 288L, 274L, 244L, 323L, 201L, 255L, 232L,
201L, 202L, 228L, 202L, 249L, 200L, 218L, 203L, 232L, 232L, 267L,
252L, 209L, 309L, 276L, 216L, 279L, 277L, 199L, 266L, 257L, 223L
), `3` = c(191L, 248L, 193L, 351L, 216L, 208L, 224L, 335L, 553L,
224L, 239L, 177L, 191L, 256L, 169L, 192L, 464L, 192L, 271L, 224L,
304L, 208L, 224L, 184L, 168L, 168L, 240L, 288L, 233L, 224L, 424L,
250L, 415L, 212L, 274L, 386L, 554L, 270L, 281L, 360L, 252L, 312L,
230L, 428L, 262L, 266L, 221L, 233L, 186L, 444L, 785L, 204L, 415L,
353L, 376L, 334L, 289L, 281L, 392L, 294L, 233L, 238L, 244L, 255L,
244L, 197L, 241L, 251L, 242L, 226L, 220L, 201L, 258L, 237L, 226L,
215L, 209L, 282L, 209L, 211L, 213L, 227L, 231L, 200L, 247L, 260L,
201L, 248L, 247L, 250L), `4` = c(192L, 320L, 232L, 248L, 216L,
248L, 249L, 353L, 528L, 231L, 257L, 232L, 184L, 305L, 192L, 224L,
607L, 216L, 328L, 359L, 497L, 240L, 248L, 192L, 240L, 168L, 288L,
296L, 223L, 208L, 889L, 207L, 283L, 298L, 404L, 262L, 407L, 219L,
264L, 228L, 248L, 1550L, 211L, 210L, 259L, 407L, 276L, 229L,
380L, 359L, 244L, 243L, 565L, 252L, 255L, 222L, 306L, 377L, 421L,
341L, 247L, 250L, 348L, 240L, 229L, 226L, 237L, 259L, 245L, 209L,
260L, 221L, 314L, 226L, 225L, 220L, 235L, 103L, 222L, 206L, 188L,
230L, 252L, 204L, 257L, 287L, 226L, 237L, 274L, 229L), `5` = c(184L,
632L, 423L, 248L, 281L, 1072L, 279L, 376L, 504L, 977L, 271L,
256L, 281L, 311L, 184L, 224L, 473L, 280L, 369L, 248L, 607L, 216L,
393L, 176L, 312L, 232L, 536L, 1617L, 200L, 200L, 271L, 268L,
286L, 288L, 448L, 396L, 290L, 233L, 284L, 295L, 301L, 374L, 225L,
190L, 346L, 284L, 395L, 539L, 262L, 473L, 341L, 190L, 646L, 229L,
428L, 264L, 365L, 565L, 286L, 271L, 234L, 498L, 309L, 332L, 229L,
216L, 251L, 227L, 225L, 215L, 259L, 235L, 295L, 231L, 232L, 211L,
250L, 216L, 208L, 191L, 190L, 235L, 221L, 378L, 250L, 234L, 244L,
256L, 250L, 233L), `6` = c(192L, 264L, 904L, 304L, 279L, 408L,
224L, 328L, 584L, 480L, 265L, 296L, 512L, 224L, 192L, 225L, 424L,
304L, 287L, 193L, 799L, 200L, 391L, 184L, 240L, 176L, 304L, 687L,
208L, 223L, 480L, 200L, 273L, 281L, 1146L, 375L, 481L, 735L,
246L, 266L, 297L, 325L, 245L, 203L, 256L, 243L, 274L, 280L, 254L,
147L, 504L, 177L, 283L, 183L, 301L, 161L, 599L, 263L, 268L, 279L,
313L, 377L, 272L, 266L, 219L, 208L, 231L, 229L, 221L, 202L, 244L,
215L, 279L, 227L, 231L, 213L, 225L, 271L, 211L, 197L, 189L, 211L,
210L, 210L, 233L, 238L, 220L, 262L, 221L, 229L), `7` = c(217L,
344L, 255L, 304L, 272L, 688L, 225L, 271L, 2759L, 11760L, 255L,
248L, 263L, 9897L, 159L, 223L, 360L, 264L, 249L, 191L, 11992L,
184L, 16273L, 209L, 176L, 185L, 232L, 12273L, 233L, 192L, 435L,
440L, 312L, 157L, 440L, 521L, 272L, 391L, 272L, 280L, 383L, 220L,
200L, 217L, 837L, 215L, 209L, 243L, 204L, 836L, 245L, 170L, 311L,
182L, 495L, 278L, 226L, 191L, 266L, 344L, 140L, 330L, 270L, 264L,
219L, 209L, 235L, 242L, 230L, 204L, 248L, 221L, 336L, 215L, 224L,
196L, 249L, 345L, 214L, 195L, 204L, 252L, 217L, 230L, 239L, 250L,
198L, 238L, 231L, 222L), `8` = c(200L, 344L, 409L, 297L, 280L,
536L, 184L, 297L, 801L, NA, 241L, 207L, 432L, NA, 184L, 225L,
415L, 688L, 343L, 200L, NA, 505L, NA, 223L, 185L, NA, 224L, NA,
279L, 9497L, 495L, 405L, 430L, 366L, 290L, 472L, 259L, 605L,
151L, 349L, 278L, 214L, 185L, 218L, 362L, 267L, 289L, 502L, 1034L,
832L, 255L, 263L, 492L, 184L, 588L, 473L, 180L, 153L, 659L, 341L,
227L, 327L, 283L, 275L, 223L, 210L, 259L, 238L, 244L, 205L, NA,
225L, NA, 223L, NA, 210L, 350L, 337L, 336L, 207L, 282L, 222L,
228L, 205L, 245L, 259L, 197L, 248L, 228L, 223L), `9` = c(200L,
335L, 431L, 488L, 321L, 472L, 176L, 287L, 831L, NA, 248L, 209L,
208L, NA, 217L, 192L, 465L, 312L, 473L, 184L, NA, 207L, NA, 216L,
215L, NA, 256L, NA, 232L, 312L, NA, 305L, 314L, 633L, 235L, 435L,
367L, 599L, 283L, 241L, 255L, 506L, 264L, 372L, 222L, 230L, 247L,
415L, 195L, 396L, 216L, 200L, 287L, 271L, NA, 302L, 302L, 279L,
NA, 259L, 235L, 398L, 250L, 274L, 208L, 201L, 252L, 227L, 228L,
253L, NA, 217L, NA, 228L, NA, 214L, NA, 333L, NA, 202L, NA, 246L,
224L, 220L, 242L, 272L, 220L, 246L, 223L, 216L), `10` = c(575L,
288L, 650L, 279L, 14367L, 472L, 264L, 385L, 385L, NA, 287L, 207L,
168L, NA, 10575L, 256L, 472L, 1088L, 400L, 777L, NA, 200L, NA,
312L, 6720L, NA, 1344L, NA, 216L, 319L, NA, 238L, 511L, NA, NA,
897L, 352L, 322L, 526L, 230L, 238L, NA, NA, 391L, 189L, 202L,
208L, 293L, 168L, 222L, 406L, 284L, 135L, NA, NA, 553L, NA, 164L,
NA, 200L, 229L, 356L, 256L, 263L, 209L, 217L, 223L, 222L, 223L,
278L, NA, 389L, NA, 245L, NA, 226L, NA, 284L, NA, 229L, NA, 623L,
218L, 224L, 252L, 246L, 235L, 250L, 216L, 217L), `11` = c(464L,
21922L, 671L, 640L, NA, 496L, 623L, 12927L, 423L, NA, 552L, 12230L,
8872L, NA, NA, 11103L, 744L, 440L, 13639L, 248L, NA, 280L, NA,
529L, NA, NA, 320L, NA, 239L, 504L, NA, 476L, 274L, NA, NA, 1276L,
131L, 430L, 633L, 198L, 271L, NA, NA, 328L, 268L, 237L, 202L,
447L, 170L, 174L, 255L, 229L, 897L, NA, NA, 262L, NA, 364L, NA,
199L, 522L, 375L, 251L, 244L, 213L, 222L, 252L, 222L, 227L, 233L,
NA, 41L, NA, 265L, NA, 217L, NA, 269L, NA, 241L, NA, 227L, 235L,
252L, 226L, 225L, 230L, 604L, 232L, 246L), `12` = c(457L, NA,
5000L, 9656L, NA, 552L, 11209L, NA, 9731L, NA, 272L, NA, 177L,
NA, NA, NA, 16304L, 432L, NA, 256L, NA, 14424L, NA, 8639L, NA,
NA, 464L, NA, 16618L, NA, NA, NA, 238L, NA, NA, NA, 289L, NA,
431L, 193L, 456L, NA, NA, NA, 206L, 257L, 198L, NA, 192L, 316L,
184L, 256L, 374L, NA, NA, 252L, NA, NA, NA, 397L, NA, 343L, 241L,
250L, 200L, 205L, NA, 258L, 247L, NA, NA, 267L, NA, 233L, NA,
207L, NA, NA, NA, NA, NA, 251L, 280L, 261L, 268L, 226L, 346L,
NA, 216L, 236L), `13` = c(687L, NA, 17098L, NA, NA, 25334L, NA,
NA, NA, NA, 10577L, NA, 192L, NA, NA, NA, NA, 22505L, NA, 320L,
NA, NA, NA, NA, NA, NA, 12808L, NA, 367L, NA, NA, NA, 235L, NA,
NA, NA, 503L, NA, NA, 187L, NA, NA, NA, NA, NA, NA, 186L, NA,
220L, 197L, NA, NA, 190L, NA, NA, 335L, NA, NA, NA, 314L, NA,
318L, 369L, 236L, 481L, 509L, NA, NA, NA, NA, NA, NA, NA, 263L,
NA, 215L, NA, NA, NA, NA, NA, 254L, NA, 274L, 255L, 222L, NA,
NA, 220L, NA), `14` = c(17242L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 24975L, NA, NA, NA, NA,
NA, NA, NA, NA, 160L, NA, NA, NA, 278L, NA, NA, NA, 326L, NA,
NA, 200L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 323L, NA, NA, 178L,
NA, NA, 449L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 234L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 226L, NA, NA, NA, NA, NA, 238L, NA,
291L, NA, 238L, NA, NA, 232L, NA), `15` = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 424L, NA, NA, NA,
658L, NA, NA, 190L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 428L,
NA, NA, 389L, NA, NA, 446L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
257L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 218L, NA, NA, NA, NA,
NA, 234L, NA, 501L, NA, 294L, NA, NA, 269L, NA), `16` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_)), row.names = c(NA,
-90L), spec = structure(list(cols = structure(list(context = structure(list(), class = c("collector_character",
"collector")), evaluationPosition = structure(list(), class = c("collector_integer",
"collector")), number = structure(list(), class = c("collector_integer",
"collector")), `1` = structure(list(), class = c("collector_integer",
"collector")), `2` = structure(list(), class = c("collector_integer",
"collector")), `3` = structure(list(), class = c("collector_integer",
"collector")), `4` = structure(list(), class = c("collector_integer",
"collector")), `5` = structure(list(), class = c("collector_integer",
"collector")), `6` = structure(list(), class = c("collector_integer",
"collector")), `7` = structure(list(), class = c("collector_integer",
"collector")), `8` = structure(list(), class = c("collector_integer",
"collector")), `9` = structure(list(), class = c("collector_integer",
"collector")), `10` = structure(list(), class = c("collector_integer",
"collector")), `11` = structure(list(), class = c("collector_integer",
"collector")), `12` = structure(list(), class = c("collector_integer",
"collector")), `13` = structure(list(), class = c("collector_integer",
"collector")), `14` = structure(list(), class = c("collector_integer",
"collector")), `15` = structure(list(), class = c("collector_integer",
"collector")), `16` = structure(list(), class = c("collector_character",
"collector")), response = structure(list(), class = c("collector_integer",
"collector")), sentence = structure(list(), class = c("collector_character",
"collector")), time_spent = structure(list(), class = c("collector_integer",
"collector")), trial_number = structure(list(), class = c("collector_integer",
"collector")), trigger = structure(list(), class = c("collector_character",
"collector")), triggerPosition = structure(list(), class = c("collector_integer",
"collector")), type = structure(list(), class = c("collector_character",
"collector"))), .Names = c("context", "evaluationPosition", "number",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "response", "sentence", "time_spent",
"trial_number", "trigger", "triggerPosition", "type")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16"))
我有90行,每行有15列,编号为1-15。这些列包含值,但并非所有行中都填充了所有15列。我想要做的是,在每一行中,循环遍历所有列,直到找到一个空列。一旦我到达那一点,我想要将列的值提取到左侧(这是该行中15列序列中的最后一个值)。
这是我尝试的解决方案:
finWord <- vector("numeric", 90L)
for (i in 1:nrow(pilotJuly)) {
for (j in 1:ncol(pilotJuly)) {
if (is.na(j)){
break
finWord[j-1] <- pilotJuly[i, as.character(pilotJuly[i, j-1])]
}
}
}
答案
也许以下功能会做你想要的。
如果行中的所有值都是NA
,则对应于该行的返回值将为NA
。
getLastNotNA <- function(X) {
apply(X, 1, function(x) {
if(anyNA(x)) {
i <- which(is.na(x))[1]
if(i > 1) x[i - 1] else NA
} else x[length(x)]
})
}
getLastNotNA(pilotJuly)
另一答案
如果您只需要具有缺失值的行中第15列的值,那么应该这样做
finWord <- x[apply(x, 1, function(x) any(is.na(x))),15]
但是如果你想从NA那里得到一个左边的值,那么
finWord <- x[is.na(x[,1]),15]
for(i in 2:ncol(x)){
finWord <- c(finWord,x[is.na(x[,i]),i-1])
}
以上是关于如何循环遍历行,然后在每行循环遍历列直到找到NA,然后提取前一列的值的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Javascript 中循环遍历 HTML 表中的复选框行