Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to list elements of a dataframe (ordered by column) with row.names using dplyr

temp_df<-data.frame(c(1,3),c(2,4))
colnames(temp_df)<-c("col1","col2")
row.names(temp_df)<-c("r1","r2")
temp_df                    

require(dplyr)
temp_df%>%split(colnames(temp_df))

Goal

col1
r1 1
r2 3
col2
r1 2
r2 4
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use base-R for this, as you're simply extracting columns from a dataframe. Using 'drop=FALSE' ensures the preservation of row names. I do hope this was your intended output (a list of one-column dataframes).

#create vector of columns
mycols <- colnames(temp_df)
names(mycols) <- mycols

#extract data
res <- lapply(mycols, function(x){temp_df[,x, drop=F]})

>res
$col1
   col1
r1    1
r2    3

$col2
   col2
r1    2
r2    4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...