使用R替换列中的NAs和其他列的值

时间:2017-09-10 03:47:00

标签: r dplyr data-manipulation

我想知道如何使用NAR中的dplyr替换为Letters <- LETTERS[1:5] Char <- c("a", "b", NA, "d", NA) df1 <- data.frame(Letters, Char) df1 library(dplyr] df1 %>% mutate(Char1 = ifelse(Char != NA, Char, Letters)) Letters Char Char1 1 A a NA 2 B b NA 3 C <NA> NA 4 D d NA 5 E <NA> NA 中其他列的值。 MWE低于。

public static void Listget (WebDriver driver) throws Exception 

{
    Thread.sleep(5000);
    UtilityMethod.getAppLocaters(driver, "closeicon").click();

    Actions action = new Actions(driver);
    WebElement we = driver.findElement(By.xpath("//li[@class='parent dropdown  aligned-left']"));
    Thread.sleep(5000);
    action.moveToElement(we).build().perform();

    List<WebElement>links = driver.findElements(By.xpath("//span[@class='menu-title']"));
    int total_count = links.size();       
    System.out.println("Total size :=" +total_count);           
     for(int i=0;i<total_count;i++)
        {             
            WebElement  element = links.get(i);
            String text = element.getAttribute("innerHTML");
            System.out.println("linksnameis:="  +text);

            try{
                    File src = new File("D:ReadFile.xlsx");
                    FileInputStream fis = new FileInputStream(src);
                    XSSFWorkbook wb=new XSSFWorkbook(fis);
                    XSSFSheet sh = wb.getSheetAt(0);

                    sh.createRow(i).createCell(1).setCellValue(text);

                    FileOutputStream fos = new FileOutputStream(new File("D:/ReadFile.xlsx"));
                    wb.write(fos);
                    fos.close();
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }


        }
    }
}

1 个答案:

答案 0 :(得分:12)

您可以使用coalesce

library(dplyr)

df1 <- data.frame(Letters, Char, stringsAsFactors = F)

df1 %>%
  mutate(Char1 = coalesce(Char, Letters))

  Letters Char Char1
1       A    a     a
2       B    b     b
3       C <NA>     C
4       D    d     d
5       E <NA>     E