使用循环简化if-else:如果存在,则赋值;如果不存在,则检查下一列

时间:2019-10-13 15:03:38

标签: r dataframe for-loop if-statement na

晚安,我想根据值是否存在来分配数据帧值。如果存在,则分配该值;如果不存在,请检查下一列; 这是我的示例数据:

portmatchfund=structure(list(crsp_portno = c(1000002, 1000020, 1000020, 1000111
), report_dt = structure(c(12325, 12325, 12325, 12325), class = "Date"), 
    actfundno1 = c(4281, NA, NA, NA), actfundno2 = c(NA, 16970, 
    16970, NA), actfundno3 = c(NA, 16971, 16971, 31020), actfundno4 = c(NA, 
    16972, 16972, NA), actfundno5 = c(4291, 16973, 16973, 31027
    ), actfundno6 = c(4301, NA, NA, NA), actfundno7 = c(4314, 
    NA, NA, 31036), actfundno8 = c(NA_real_, NA_real_, NA_real_, 
    NA_real_), actfundno9 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno10 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno11 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno12 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno13 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno14 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno15 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno16 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno17 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno18 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno19 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno20 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno21 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno22 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno23 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno24 = c(NA_real_, NA_real_, NA_real_, NA_real_
    ), actfundno25 = c(NA_real_, NA_real_, NA_real_, NA_real_
    )), row.names = c("39", "569", "5691", "3718"), class = "data.frame")

我尝试了if-else语句来解决这个问题。但是,我的列太多了,ifelse方法很傻,这是傻方法:

portmatchfund$V28=ifelse(!is.na(portmatchfund$actfundno1),portmatchfund$actfundno1,                               
       ifelse(!is.na(portmatchfund$actfundno2),portmatchfund$actfundno2,               
              ifelse(!is.na(portmatchfund$actfundno3),portmatchfund$actfundno3,
                     ifelse(!is.na(portmatchfund$actfundno4),portmatchfund$actfundno4,                               
                            ifelse(!is.na(portmatchfund$actfundno5),portmatchfund$actfundno5,               
                                   ifelse(!is.na(portmatchfund$actfundno6),portmatchfund$actfundno6,
                                          ifelse(!is.na(portmatchfund$actfundno7),portmatchfund$actfundno7,                               
                                                 ifelse(!is.na(portmatchfund$actfundno8),portmatchfund$actfundno8,               
                                                        ifelse(!is.na(portmatchfund$actfundno9),portmatchfund$actfundno9,
                                                               ifelse(!is.na(portmatchfund$actfundno10),portmatchfund$actfundno10,                               
                                                                      ifelse(!is.na(portmatchfund$actfundno11),portmatchfund$actfundno11,               
                                                                             ifelse(!is.na(portmatchfund$actfundno12),portmatchfund$actfundno12,
                                                                                    ifelse(!is.na(portmatchfund$actfundno13),portmatchfund$actfundno13,                               
                                                                                           ifelse(!is.na(portmatchfund$actfundno14),portmatchfund$actfundno14,               
                                                                                                  ifelse(!is.na(portmatchfund$actfundno15),portmatchfund$actfundno15,
                                                                                                         ifelse(!is.na(portmatchfund$actfundno16),portmatchfund$actfundno16,                               
                                                                                                                ifelse(!is.na(portmatchfund$actfundno17),portmatchfund$actfundno17,               
                                                                                                                       ifelse(!is.na(portmatchfund$actfundno18),portmatchfund$actfundno18,
                                                                                                                              ifelse(!is.na(portmatchfund$actfundno19),portmatchfund$actfundno19,                               
                                                                                                                                     ifelse(!is.na(portmatchfund$actfundno20),portmatchfund$actfundno20,               
                                                                                                                                            ifelse(!is.na(portmatchfund$actfundno21),portmatchfund$actfundno21,
                                                                                                                                                   ifelse(!is.na(portmatchfund$actfundno22),portmatchfund$actfundno22,                               
                                                                                                                                                          ifelse(!is.na(portmatchfund$actfundno23),portmatchfund$actfundno23,               
                                                                                                                                                                 ifelse(!is.na(portmatchfund$actfundno24),portmatchfund$actfundno24,
                                                                                                                                                                        ifelse(!is.na(portmatchfund$actfundno25),portmatchfund$actfundno25,portmatchfund$V28)))))))))))))))))))))))))    

我想找到一种方法来简化该愚蠢的代码。在我的示例中,结果应为4281,16970,16970,31020。

感谢您的帮助,祝您愉快。

0 个答案:

没有答案