Exporting a CSV with specific columns

Hello All,

I have a question regarding exporting a DF to a csv.

The question is: How can I export only specific columns in the DF to a csv file?

I know how to import specific columns, but I can't seem to figure out how to export specific ones.

Example of importing specific columns below:

DF3 <- read.csv("September_Service_Utilization.csv", sep = ",")[,c("Gender", "Age")]

I have tried using select with dplyr / pipe operator to get specific columns that way or have even created a list, but can't seem to get anything to work.

For example, in the DF, I want to export only "Age", "Gender", "ID",

How would I go upon doing that?

All the help is greatly appreciated.

I did not know it a reproduceable table would be needed with such a question / need for help as this scenario.

Thank you!!

Something like this should work.

library(dplyr)
DF <- data.frame(Age=34:36,Value=1:3,Gender=c("F","F","M"),Location=0:2,
                 ID=c("A","B","C"))
DF |> select(Age,Gender,ID) |> write.csv(file = "AgeGenderID.csv",row.names = FALSE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.