logo
Tags down

shadow

Isolate data frames from a spreadsheet to create a list


By : Aya Sherwani
Date : October 17 2020, 08:10 AM
seems to work fine How do I isolate data frames in R from a large spreadsheet of data and create a list from the isolated dataframes?
code :
df <- structure(list(group = structure(c(2L, 1L, 1L, 3L, 1L, 4L, 1L, 
                                         1L), .Label = c("", "Group1", "Group2", "Group3"), class = "factor"), 
                     year = c(2013L, 2014L, 2015L, 2013L, 2014L, 2013L, 2014L, 
                              2015L), NSW = c(500L, 601L, 410L, 508L, 694L, 305L, 122L, 
                                              210L), VIC = c(501L, 520L, 435L, 501L, 780L, 501L, 440L, 
                                                             235L), QLD = c(985L, 999L, 894L, 806L, 899L, 985L, 704L, 
                                                                            523L)), class = "data.frame", row.names = c(NA, -8L))

df_list <- split(df, as.factor(df$group))


Share : facebook icon twitter icon

Create a sublist within a list of data frames in terms of the dimension of the data frames


By : Xavier Borrull Cabal
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a list of data frames that have different dimensions. I want to create different alternative sublists that contain data frames with the same number of columns. , It's very easily solved using
code :
split(df_list, lengths(df_list)) 
# or for older R versions: split(df_list, sapply(df_list, ncol))
l <- list(
  data.frame(x = 1),
  data.frame(x = 1, y = 2),
  data.frame(x = 1),
  data.frame(x = 1, y = 2, z = 3),
  data.frame(x = 1))
lengths(l)
#[1] 1 2 1 3 1
res <- split(l, lengths(l))
str(res)
#List of 3
# $ 1:List of 3
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
# $ 2:List of 1
#  ..$ :'data.frame':   1 obs. of  2 variables:
#  .. ..$ x: num 1
#  .. ..$ y: num 2
# $ 3:List of 1
#  ..$ :'data.frame':   1 obs. of  3 variables:
#  .. ..$ x: num 1
#  .. ..$ y: num 2
#  .. ..$ z: num 3

For Loop Over List of Data Frames and Create New Data Frames from Every Iteration Using Variable Name


By : Lee ByeongChan
Date : March 29 2020, 07:55 AM
hope this fix your issue If I understand correctly based on your intended result, maybe using the match_df could be an option.
code :
library(plyr)
dupl.john <- match_df(john, bcm)
dupl.jane <- match_df(jane, bcm)
library(plyr)
l <- list(john, jane)
res <- lapply(l, function(x) {match_df(x, bcm, on = "Full.Name")} )
dupl.john <- as.data.frame(res[1])
dupl.jane <- as.data.frame(res[2])

how to create a new list of data.frames by systematically rearranging columns from an existing list of data.frames


By : Nastia
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a list of about 100 data frames. I would like to create a new list of data frames where the first data frame is made up of the first columns of all the existing data frames, and the second data frame is made up of the second column etc... , This is an option:
code :
cols <- ncol(my_lst[[1]])

lapply(1:cols, function(x) do.call(cbind, lapply(my_lst, `[`, x)))

[[1]]
  a c b
1 0 1 0
2 0 1 1
3 1 0 0
4 1 0 0
5 1 1 1

[[2]]
  b b a
1 0 0 0
2 1 1 0
3 0 0 1
4 0 0 1
5 1 1 1

[[3]]
  c a c
1 1 0 1
2 1 0 1
3 0 1 0
4 0 1 0
5 1 1 1

How do I use a for loop to create a list of data frames, based off two existing data frames, in R?


By : user3494438
Date : March 29 2020, 07:55 AM
around this issue You had a small mistake in for loop. You had to use [[ instead of [ while accessing the list. You may want to read up ?Extract if you are interested in different ways of accessing elements.
code :
for(i in seq_along(sppcodes)){

   sppn <- sppcodes[i]
   plot = df_obs$plot
   obs = df_obs[,sppn]
   pred = df_preds[,sppn]
   df <- data.frame(plot, obs, pred) 
   test.eval.list[[sppn]] <- df 
}
Map(cbind.data.frame, plot = list(df_obs$plot),obs=df_obs[-1],pred = df_preds[-1])

#[[1]]
#   plot obs      pred
#1     1   1 0.3266487
#2     2   1 0.3745092
#3     3   0 0.8633161
#4     4   0 0.1970302
#5     5   1 0.3017755
#6     6   0 0.9154151
#7     7   0 0.6193044
#8     8   0 0.4020479
#9     9   1 0.9947362
#10   10   1 0.7975380
#...
#....

Create list of data.frames with specific rows from list of data.frames


By : user3723118
Date : March 29 2020, 07:55 AM
wish of those help I have a number of data.frames stored in a list (list1) and would like to create a new list (list2) with data.frames where the first contains all the first rows of the data.frames in list1, second contains all the second rows etc. Here is an example: , If all have the same number of rows
code :
nr <- nrow(list1[[1]])
lapply(seq_len(nr), function(i) do.call(rbind, lapply(list1, function(x) x[i,])))
library(dplyr)
library(data.table)
bind_rows(list1, .id = 'grp') %>%
    mutate(rn = rowid(grp)) %>%
   {split(.[c('a', 'b')], .$rn)}
Related Posts Related Posts :
  • Error connecting to mongoDB using Mongolite
  • R: networkD3 sankey plot - colours not displaying
  • Merging two rows into one header using R
  • Parse JSON to dataframe returns different numbers
  • How to number by group?
  • Multinomial probit regression with mixed type explanatory variables
  • How can I make a variable in a dataset containing a vector of all numbers between two other variables?
  • How to extract the trailing digits from a string in R?
  • Select values based on other columns
  • readLines killing R in purrr::map
  • Subset rows based on "start and stop" strings
  • How to add a column to lists within a list without losing their names?
  • Plotting the means in ggplot, without using stat_summary()
  • R :Looping through each 5 rows of data frame and imputing incremental value
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org