Search code examples
rdatasetexport-to-excel

What R function can help me to select and export a column from a dataframe?


I have already imported my SAS dataset to R with the haven package. Now I need to extract some of the columns from the data and export them to an excel file.


Solution

  • If you already imported the file to R, let's assume that you saved it in a dataframe called df.

    Then, you can select the rows you want, and save the results to a new dataframe called df2 with the select command from the tidyverse package. Just put the variables' names you want separeted by commas:

    library(tidyverse)
    
    df2 <- df %>% select(var1, var2, var3)
    

    After that, you can export the new dataframe to a .csv file, which you may open in Excel:

    write.csv(df2,"C:\\Users\\Ary\\Desktop\\MyData.csv", row.names = FALSE)