0 votes
1 view
in R Programming by (6.5k points)

I have an R data frame with 6 columns, and I want to create a new data frame that only has three of the columns.

Assuming my data frame is df, and I want to extract columns A, B, and E, this is the only command I can figure out:

 data.frame(df$A,df$B,df$E)

Is there a more compact way of doing this?

1 Answer

0 votes
by (25.3k points)

To extract specific columns from a data frame, you can use the select() function from the dplyr package as follows:

select(dataframe, A, B, E)

OR

dataframe %>%

  select(A, B, E)

You can also use the subset() function as follows:

subset(dataframe, select=c("A", "B", "E"))

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...