Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

how to convert a large dataframe based on columns into a list of dataframes in R

$
0
0

i have a large dataframe with 1365 columns as shown in the example:

df <- data.frame(X1 = c(7.48, 7.59), Y1 = c(49.16, 48.70), ID1 = c("B_1", "B_1_2"), TI1 = c(191.31, 1349.93), 
              X2 = c(8.15, 8.06), Y2 = c(48.40, 48.18), ID2 = c("B_1", "B_1_2"), TI2 = c(191.39, 1349.97),
              X3 = c(8.80, 8.87), Y3 = c(47.65, 47.20), ID3 = c("B_1", "B_1_2"), TI3 = c(191.48, 1350.05))

now I would like to split this dataframe into a list of dataframes (i.e. three dataframes in this list; 12/4 = 3). the expected list that looks like as follows:

dflist[[1]]

X1    Y1     ID1     TI1   
7.48  49.16  B_1    191.31
7.59  48.70  B_1_2  1349.93

dflist[[2]]

X2     Y2    ID2     TI2 
8.15   48.40   B_1    191.39
8.06   48.18   B_1_2  1349.97

dflist[[3]]

X3     Y3    ID3     TI3
8.80   47.65   B_1    191.48 
8.87   47.20   B_1_2  1350.05

and i would like to rearrange (in ascending order) the data in each dataframe of the list based on column 4 of each dataframe

first to convert the dataframe into list, i used the following code but not working

n <- seq(1,12, by = 4)

my_data <- list()
for (i in 3) {
  for (j in seq(1, 12, by = 4)) {

    my_data[[i]] <- df3[, j:j+3]

  }
}

for sorting the list the following code is used:

dflist1 <- lapply(dflist, function(1){
            1[with(dflist, order[, 4]), ]
           })

looking for the code to get the expected output (dflist) as presented above


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>