logo
down
shadow

Merging two rows into one header using R


Merging two rows into one header using R

By : Sreedeep Moulik
Date : October 23 2020, 08:10 PM
hope this fix your issue I guess you need to paste the first row in header and then delete the row
code :
names(df) <- paste(names(df), df[1, ], sep = "_")
df[-1,]
df[] <- lapply(df, as.character)


Share : facebook icon twitter icon
Merging rows within a data frame and merging columns between data frames

Merging rows within a data frame and merging columns between data frames


By : Matan Cohen
Date : March 29 2020, 07:55 AM
With these it helps It is probaly best to summarise first and then merge / join the two datasets. With the table-format of table 2 you can also use the melt and dcast functions of reshape2 with sum as aggregate function (which results in a dataframe) and then merge with the aggregated t1 dataframe:
code :
library(reshape2)
# aggragate 't1'
t1sum <- aggregate(.~Town, t1, sum)
# reshape and aggregate 't2'
t2sum <- dcast(melt(t2), Town ~ Species, fun.aggregate = sum)
# or with 'as.data.frame(t2)' instead of 'melt(t2)'
t2sum <- dcast(as.data.frame(t2), Town ~ Species, fun.aggregate = sum)

merge(t1sum, t2sum, by = 'Town')
  Town Captured Proportion funestus gambiae indeterminada outro pharoenois tenebrosus
1    A      184       0.25      113      44             1     2          2         22
2    B      243       0.33        5     234             0     0          1          3
3    C       12       0.02        4       6             0     0          0          2
4    D       32       0.04        8      13             0     0          1         10
5    E      240       0.33       22     206             0     0          2          7
6    F       26       0.04        8      11             0     0          0          7
library(data.table)
t1dt <- setDT(t1)[, lapply(.SD, sum), by = Town]
t2dt <- dcast(setDT(melt(t2)), Town ~ Species, fun.aggregate = sum)

t1dt[t2dt, on='Town']
t1 <- structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Captured = c(168L, 16L, 243L, 12L, 17L, 15L, 7L, 233L, 14L, 12L), Proportion = c(0.23, 0.02, 0.33, 0.02, 0.02, 0.02, 0.01, 0.32, 0.02, 0.02)), class = "data.frame", .Names = c("Town", "Captured", "Proportion"), row.names = c(NA,-10L))
t2 <- structure(c(106L, 7L, 5L, 4L, 4L, 4L, 4L, 18L, 5L, 3L, 38L, 6L, 234L, 6L, 8L, 5L, 3L, 203L, 4L, 7L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 0L, 20L, 2L, 3L, 2L, 5L, 5L, 0L, 7L, 5L, 2L), .Dim = c(10L, 6L), .Dimnames = structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Species = c("funestus", "gambiae", "indeterminada", "outro", "pharoenois", "tenebrosus")), .Names = c("Town", "Species")), class = "table")
'Merging' multiple rows with NULLs into rows with distinct sums

'Merging' multiple rows with NULLs into rows with distinct sums


By : Ahmed
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Imagine the following table , One way would be
code :
SELECT COALESCE(T1.EEID, T2.EEID) AS EEID,
       CODE1,
       VALUE1,
       CODE2,
       VALUE2
FROM   (SELECT EEID,
               CODE1,
               SUM(VALUE1)                        AS VALUE1,
               ROW_NUMBER() OVER (ORDER BY CODE1) AS RN
        FROM   YourTable
        WHERE  CODE1 IS NOT NULL
        GROUP  BY EEID,
                  CODE1) T1
       FULL JOIN (SELECT EEID,
                         CODE2,
                         SUM(VALUE2)                        AS VALUE2,
                         ROW_NUMBER() OVER (ORDER BY CODE2) AS RN
                  FROM   YourTable
                  WHERE  CODE2 IS NOT NULL
                  GROUP  BY EEID,
                            CODE2) T2
         ON  ON T1.RN = T2.RN AND T1.EEID = T2.EEID;
SELECT EEID,
       MAX(CODE1)  AS CODE1,
       SUM(VALUE1) AS VALUE1,
       MAX(CODE2)  AS CODE2,
       SUM(VALUE2) AS VALUE2
FROM   (SELECT EEID,
               CODE1,
               VALUE1,
               CODE2,
               VALUE2,
               DENSE_RANK() OVER (PARTITION BY EEID ORDER BY CASE WHEN CODE1 IS NULL THEN 1 ELSE 0 END, Code1) AS RN1,
               DENSE_RANK() OVER (PARTITION BY EEID ORDER BY CASE WHEN CODE2 IS NULL THEN 1 ELSE 0 END, Code2) AS RN2
        FROM   YourTable) T
GROUP  BY EEID,
          CASE
            WHEN CODE1 IS NULL
              THEN RN2
            ELSE RN1
          END
HAVING COALESCE(MAX(CODE1), MAX(CODE2)) IS NOT NULL; 
Removing rows based on other rows and merging the remaining rows

Removing rows based on other rows and merging the remaining rows


By : WebandSteak
Date : March 29 2020, 07:55 AM
Hope that helps I am having some trouble trying to get a table to show only the information that I want. I am trying to get the below table to only show ID's which have no true statements (1's) for either column 1 or column 2 , I think this should resolve your question
code :
SELECT * 
FROM   input_table 
WHERE  id IN (SELECT Max(id) 
              FROM   input_table 
              GROUP  BY id 
              HAVING Sum(column2) < 1 
                     AND Sum(column3) < 1);
Merging pandas dataframes, alternating rows without soritng rows

Merging pandas dataframes, alternating rows without soritng rows


By : user3267913
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You can use helper MultiIndex with np.arange and DataFrame.set_index with append=True, add keys parameter for P, r values, sorting by ranges, remove this level and last change order of levels by DataFrame.swaplevel:
code :
s1 = pvals.set_index(np.arange(len(pvals)), append=True)
s2 = rs.set_index(np.arange(len(rs)), append=True)

df = (pd.concat([s1, s2], keys=('P','r'))
        .sort_index(kind='merge', level=2)
        .reset_index(level=2, drop=True)
        .swaplevel(0,1))
print (df)
         T     4    Rw    Af
T  P  0.00  0.05  0.24  0.01
   r  1.00  0.65  0.28  0.44
4  P  0.05  0.00  0.76  0.03
   r  0.65  1.00  0.01  0.03
Rw P  0.24  0.76  0.00  0.44
   r -0.03  0.01  1.00  0.32
s1 = pvals.assign(a = np.arange(len(pvals))).set_index('a', append=True) 
s2 = rs.assign(a = np.arange(len(rs))).set_index('a', append=True)

df = (pd.concat([s1, s2], keys=('P','r'))
        .sort_index(kind='merge', level=2)
        .reset_index(level=2, drop=True)
        .swaplevel(0,1))
Merging multiple rows per group into one while merging only some column data in R

Merging multiple rows per group into one while merging only some column data in R


By : user3309747
Date : March 29 2020, 07:55 AM
it helps some times I believe you can just use a summary statement like post_datapoint_xyz = first(post_datapoint_xyz) for each of the post-columns to address your problem.
What also works would be to simply include all the post columns into your group_by list :)
code :
all_reddits <- all_posts_and_comments %>%
  group_by_at(vars(starts_with('post_'))) %>%
  summarise(...)
all_reddits <- all_posts_and_comments %>%
  nest(starts_with('comment_'), .key = 'comments')
all_posts_and_comments.2 <- all_reddits %>%
  unnest(comments)
Related Posts Related Posts :
  • how to loop for division funciton in r
  • Why does ggplot not allow suppressing of messages generated by its geoms?
  • Download multiple excel files linked through urls in R
  • sparklyr : spark_apply function is not working in cluster mode
  • dplyr mutate - How do I pass one row as a function argument?
  • R selecting rows by conditions given in an external table
  • Native regex way to replace multiple leading chars with equal number spaces
  • stan - difficulty vectorizing
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org