I've created 15 individual dataframes via a for loop, each one representing scores for each of the survey items comprising 15 different scales/topic areas. They're named df1 through df15 based on the original order of my topic areas, but I'd like to put them into a single list based on a different sort order. I have a separate dataframe, called topic.area, listing each of the 15 topic areas, their original order, and it is arranged in the new sort order:
Topic.Area...... orig.order... sort.order
Leadership........3................1
Engagement..... 1...............2
Innovation.........2................3
etc...
I essentially want to do the following, but the code isn't working for me. I'm very new to R, so I appreciate any solutions that aren't too advanced so I understand the logic behind them.
myList<- list()
for(m in 1:15) {
myList[[m]]<- paste("df", topic.area$orig.order[m], sep = "")
}
I think the issue is you can't use the paste function in an assignment command, but I don't know to work around that.
Get all the dataframe in a list. As you have mentioned this is already ordered by orig.order
.
list_df <- mget(paste0('df', 1:15))
You can arrange them based on sort.order
directly by doing:
sorted_list_df <- list_df[topic.area$sort.order]
Here is a completely reproducible example based on Pokémon Stats data. Instead of just writing the input files to a list()
, I use assign()
to simulate a set of data frames in the global environment.
download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
"pokemonData.zip",
method="curl",mode="wb")
unzip("pokemonData.zip")
thePokemonFiles <- list.files("./pokemonData",
full.names=TRUE)
filenames <- substr(list.files("./pokemonData",full.names = FALSE),1,5)
results <- lapply(1:length(thePokemonFiles),function(x) {
# read data and assign to a name in global environment
data <- read.csv(thePokemonFiles[x],stringsAsFactor = FALSE)
assign(filenames[x],data,globalenv())
return(TRUE)
})
At this point we have 7 data frames named gen01
to gen07
in the global environment.
Next, we generate the desired sort order as a vector. We'll use this along with the names from the filenames
vector to get the data and build the output list in the desired sort order of data frames.
desiredSortOrder <- c(1,3,5,7,2,4,6)
sortedDataFrames <- lapply(desiredSortOrder,function(x){
get(filenames[x])
})
# verify generation numbers by printing first row of each data frame
lapply(sortedDataFrames,function(x){
x[1,c(1,2,12)]
})
Since the Generation
column in each data frame matches its associated file name, we can verify the sort order by printing the first row of each data frame.
>lapply(sortedDataFrames,function(x){
+ x[1,c(1,2,12)]
+ })
[[1]]
Number Name Generation
1 1 Bulbasaur 1
[[2]]
Number Name Generation
1 252 Treecko 3
[[3]]
Number Name Generation
1 494 Victini 5
[[4]]
Number Name Generation
1 722 Rowlet 7
[[5]]
Number Name Generation
1 152 Chikorita 2
[[6]]
Number Name Generation
1 387 Turtwig 4
[[7]]
Number Name Generation
1 650 Chespin 6
>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.