Monday, 28 August 2017

r - Extracting specific columns from a data frame



I have an R data frame with 6 columns, and I want to create a new dataframe 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?


Answer



Using the dplyr package, if your data.frame is called df1:



library(dplyr)

df1 %>%
select(A, B, E)



This can also be written without the %>% pipe as:



select(df1, A, B, E)

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...