logo
Tags down

shadow

merge 2 data frames in a loop for each column in one of them


By : Renuka Aggarwal
Date : October 17 2020, 08:10 PM
around this issue We could use lapply over all the columns of mydf and match the value in mydf with that code column in codetable and get corresponding translation.
code :
mydf[] <- lapply(mydf, function(x) codetable$translation[match(x, codetable$code)])

mydf
#   a b c
#1  H F F
#2  H F J
#3  A I I
#4  H G C
#5  A E G
#6  G D H
#7  J G C
#8  H B G
#9  J I F
#10 C I B
#.....
set.seed(234)
mydf <- data.frame(a=sample(1:10, 20, replace=T), 
                   b=sample(1:10, 20, replace=T), 
                   c=sample(1:10, 20, replace=T))


Share : facebook icon twitter icon

Merge and name data frames in for loop


By : Maxim Elets
Date : March 29 2020, 07:55 AM
seems to work fine I have a bunch of DF named like: df1, df2, ..., dfN , If your data frames are in a list, writing a for loop is trivial:
code :
# lt = list(lt1, lt2, lt3, ...)
# if your data is very big, this may run you out of memory
lt = lapply(ls(pattern = "lt[0-9]*"), get)

merged_data = merge(lt[[1]], lt[[2]])


for (i in 3:length(lt)) {
    merged_data = merge(merged_data, lt[[i]])
    save(merged_data, file = paste0("merging", i, ".rda"))
}

Merge Two data frames column by column. (creating a table out of two data frames)


By : davidel323
Date : March 29 2020, 07:55 AM
it fixes the issue We can use order on the sequence of columns, then get the names of dataset based on the order, rbind with the 'Date' column to create a vector of column names. This can be used to order the columns in the full dataset (cbind(df1, df2))
code :
cbind(df1, df2)[c(rbind('Date', matrix(c(names(df1)[-1], 
       names(df2)[-1])[order(c(seq_along(df1)[-1], seq_along(df2)[-1]))], ncol=2)))]
#      Date customer1 order1 Date.1 customer2 order2
#1    1         6      0      1         2      0
#2    2         7      1      2         3      1
#3    3         8      3      3         6      0
#4    4         4      0      4         5      0
#5    5         2      1      5         3      2

Changing Column names of multiple data frames using a for loop with data frames loaded into a List


By : Er Dileep kumar
Date : March 29 2020, 07:55 AM
hope this fix your issue I am trying to change the variable names in all data frames in a for loop. Any example of the data is:
code :
L <- lapply(L, function(x){
  colnames(x) <- c("NewName1", "NewName2")
  x
} )

How do I merge two data frames if a column and it's values exist in both data frames?


By : Antonio Faillace
Date : March 29 2020, 07:55 AM
this one helps. IIUC, you can do df.combine_first() which
code :
df1.set_index('A').combine_first(df2.set_index('A')).reset_index()
        A       B      C
0  'name1'   'foo'  'bar'
1  'name2'  'foo''  'bar'
2  'name3'   'foo'  'bar'
3  'name4'   'foo'  'bar'

how to merge data across data.frame rows based on identical column values, for data.frames with multiple column formats


By : Josh Max Bacarac Gui
Date : March 29 2020, 07:55 AM
it should still fix some issue I have raw data in the format of df below that I'm needing to condense into a fewer row data.frame. The data represent eventual GIS point data (columns for coordinates are left off here) so I'm hoping to avoid plotting any duplicate points. Each row represents one point for which either or both of agency1 and agency2 publish data. A simpler case with fewer column formats is answered here. , You can make use of this. Might not be the most concise solution.
code :
# Data with NA values in column - agency3_id
df_na <- df[is.na(df$agency3_id), ]

# Logic
df <- df[!is.na(df$agency3_id), ] %>% 
  group_by(agency3_id) %>% 
  summarise_all(list(~ if(all(is.na(.))) NA else .[!is.na(.)][1]))

# Merge dataframes
rbind(df, df_na)
# Result
# A tibble: 5 x 9
  agency3_id agency1_id agency2_id agency1_val agency2_val agency1_desc agency2_desc agency1_dtm        
* <fct>           <dbl> <fct>            <dbl>       <dbl> <fct>        <fct>        <dttm>             
1 abcde            2007 xcv               0.23        6.3  ko           vf           2019-05-30 04:35:00
2 ertyu              NA zxc               1.5         4.4  NA           lo           NA                 
3 qwert            1500 NA                0.21        0.05 st           NA           2019-05-30 04:30:00
4 NA               4501 NA                4.3        NA    ui           NA           2019-05-30 04:33:00
5 NA                 NA bnm              NA           2    NA           kl           NA                 
# … with 1 more variable: agency2_dtm <dttm>
Related Posts Related Posts :
  • Get the min value of one variable greater than the observation of another variable
  • Reference previous value in data.table calculation
  • 'Can't create call to non-callable object' error while summarizing data by grouping
  • Multiplying all columns in dataframe by single column
  • Calculating Standard Deviation by each unique variable in R.
  • Fisher exact test for each gene
  • Sorting coordinates to create a polygon gives messy results
  • Merging legends in ggplot fails with certain expressions
  • Shiny modal dialog in response to multiple buttons
  • 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?
  • 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
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org