R extract data frame from list without prefixes in column names -
i place data frame inside list. when try extract - column names prefixed list key data frame, there way extract data frame passed initially?
cols<-c("column1", "column2", "column3") df1<-data.frame(matrix(ncol = 3, nrow = 1)) colnames(df1)<-cols df1 result<-list() result['df1']<-list(df1) newdf1<-as.data.frame(result['df1']) newdf1
get result (column names prefixed df1):
> cols<-c("column1", "column2", "column3") > df1<-data.frame(matrix(ncol = 3, nrow = 1)) > colnames(df1)<-cols > df1 column1 column2 column3 1 na na na > > result<-list() > result['df1']<-list(df1) > > newdf1<-as.data.frame(result['df1']) > newdf1 df1.column1 df1.column2 df1.column3 1 na na na
of course, can remove prefixes manually, there proper way this. thanks!
extract using [[
rather [
:
> newdf1 <- as.data.frame(result[['df1']]) > newdf1 column1 column2 column3 1 na na na
the difference [
extracts list containing requested component(s). [[
extracts requested component directly (i.e. retrieves contents of component of list, not list containing component).
but df1
is data frame, why not do:
newdf1 <- result[['df1']]
? don't need as.data.frame()
part.
Comments
Post a Comment