logo
down
shadow

Best way to combine and keep columns


Best way to combine and keep columns

By : user2953783
Date : November 21 2020, 01:01 AM
this will help I am trying to reshape a data frame. It has 4 columns: I want to combine 3 of them and keep one. It's probably easier to understand by an example: , You could try
code :
data.frame(year=rep(df$year,each=length(df)-1),x1=c(t(df[,-1])))
library(reshape2)
melt(df, id.var='year')[,-2]


Share : facebook icon twitter icon
How to combine rows after a JOIN where duplicate first columns occur, but differing information in the other columns

How to combine rows after a JOIN where duplicate first columns occur, but differing information in the other columns


By : D.M.J.
Date : March 29 2020, 07:55 AM
hope this fix your issue I am having a problem with the way the results are outputted to a HTML table after combining the data of two sql tables. , A GROUP BY is the way to go. Try this:
code :
SELECT stationname, address, manager,
MIN(score1) as score1,
MIN(score2) as score2,
MIN(score3) as score3
FROM table1 
LEFT JOIN table2 
ON table1.stationName = table2.stationName
GROUP BY stationname, address, manager
How to efficiently combine two columns into one column/ combine strings?

How to efficiently combine two columns into one column/ combine strings?


By : Jemmy Wu
Date : March 29 2020, 07:55 AM
it helps some times You have to explicitly case the Timestamp to a string e.g. with strftime:
code :
In [11]: df = pd.DataFrame([[pd.Timestamp("2017-01-01"), 'a'], [pd.Timestamp("2017-01-02"), 'b']], columns=["A", "B"])

In [12]: df["A"].dt.strftime("%Y-%m-%d") + df["B"]
Out[12]:
0    2017-01-01a
1    2017-01-02b
dtype: object
Excel/VBA - Combine rows and columns from worksheets into one, with varying source columns

Excel/VBA - Combine rows and columns from worksheets into one, with varying source columns


By : mateo
Date : March 29 2020, 07:55 AM
around this issue I have made some changes to your code, to make it suitable for mapping any column from master to sheet1. You have to hard code the mapping inside the code Sub CombineData() Dim Sht As Worksheet Dim colname As String Dim Lastrow As Integer, rowcount As Integer 'This If will clear Master before combining Worksheets("Master").Range("A2:ZZ9000").ClearContents colname = 1 For Each Sht In ActiveWorkbook.Worksheets
code :
  If Sht.Name = "Sheet2" And Sht.Range("A2").Value <> "" Then
     Lastrow = Range("A9000").End(xlUp).Row
     Sheets("Master").Select
     rowcount = Range("A9000").End(xlUp).Row
     Sht.Select
'Map the columns of sheet2 to master
     Sheets("Master").Range("A" & rowcount & ":A" & rowcount + Lastrow - 2).Value = Sht.Range("A2:A" & Lastrow).Value
     Sheets("Master").Range("B" & rowcount & ":B" & rowcount + Lastrow - 2).Value = Sht.Range("C2:C" & Lastrow).Value

  ElseIf Sht.Name = "Sheet3" And Sht.Range("A2").Value <> "" Then
     Lastrow = Range("A9000").End(xlUp).Row
     Sheets("Master").Select
     rowcount = Range("A9000").End(xlUp).Row
     Sht.Select
'Map the columns of sheet3 to master
     Sheets("Master").Range("A" & rowcount & ":A" & rowcount + Lastrow - 2).Value = Sht.Range("A2:A" & Lastrow).Value
     Sheets("Master").Range("B" & rowcount & ":B" & rowcount + Lastrow - 2).Value = Sht.Range("B2:B" & Lastrow).Value
  End If
 Next Sht

End Sub
Sub CombineData()
Dim Sht As Worksheet
Dim colname As String
 Dim Lastrow As Integer, rowcount As Integer
'This If will clear Master before combining
Worksheets("Master").Range("A2:ZZ9000").ClearContents
 colname = 1
For Each Sht In ActiveWorkbook.Worksheets
  If Sht.Name = "Sheet1" And Sht.Range("A2").Value <> "" Then
Sheets("Sheet1").Select

Lastrow = Range("A9000").End(xlUp).Row

     Sheets("Master").Select
     rowcount = Range("A9000").End(xlUp).Row + 1
     Sht.Select
'Map the columns of sheet2 to master
     Sheets("Master").Range("A" & rowcount & ":A" & rowcount + Lastrow - 2).Value = Sht.Range("A2:A" & Lastrow).Value
     Sheets("Master").Range("B" & rowcount & ":B" & rowcount + Lastrow - 2).Value = Sht.Range("B2:B" & Lastrow).Value
     Sheets("Master").Range("C" & rowcount & ":C" & rowcount + Lastrow - 2).Value = Sht.Range("C2:C" & Lastrow).Value
     Sheets("Master").Range("D" & rowcount & ":D" & rowcount + Lastrow - 2).Value = Sht.Range("D2:D" & Lastrow).Value

  ElseIf Sht.Name = "Sheet2" And Sht.Range("A2").Value <> "" Then
Sheets("Sheet2").Select

     Lastrow = Range("A9000").End(xlUp).Row
     Sheets("Master").Select
     rowcount = Range("A9000").End(xlUp).Row + 1
     Sht.Select
'Map the columns of sheet3 to master
     Sheets("Master").Range("A" & rowcount & ":A" & rowcount + Lastrow - 2).Value = Sht.Range("A2:A" & Lastrow).Value
     Sheets("Master").Range("E" & rowcount & ":E" & rowcount + Lastrow - 2).Value = Sht.Range("B2:B" & Lastrow).Value
     Sheets("Master").Range("F" & rowcount & ":F" & rowcount + Lastrow - 2).Value = Sht.Range("C2:C" & Lastrow).Value
     Sheets("Master").Range("G" & rowcount & ":G" & rowcount + Lastrow - 2).Value = Sht.Range("D2:D" & Lastrow).Value
     Sheets("Master").Range("C" & rowcount & ":C" & rowcount + Lastrow - 2).Value = Sht.Range("E2:E" & Lastrow).Value

 End If
 Next Sht

End Sub
How to combine multiple character columns into one columns and remove NA without knowing column numbers

How to combine multiple character columns into one columns and remove NA without knowing column numbers


By : user2545196
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I would like to have a column that contains other columns characters without NA. I have tried paste, str_c and unite, but could not get the expected result. Maybe I used them incorrectly. , Here is a base R method
code :
input$ALL <- apply(input[-1], 1, function(x) paste(na.omit(x), collapse=" "))
input$ALL
#[1] "tv"     "web"    "book"   "web tv"
Find rows in data frame with certain columns are duplicated, then combine the the elements in other columns

Find rows in data frame with certain columns are duplicated, then combine the the elements in other columns


By : user2551654
Date : March 29 2020, 07:55 AM
To fix this issue This is a tidyverse based solution where you can use paste with collapse after grouping it.
code :
library(dplyr)
DF = cbind.data.frame(A = c(1, 1, 2, 3, 3), 
                      B = c("a", "b", "a", "c", "c"), 
                      C = c("M", "N", "X", "M", "N"))


DFE = cbind.data.frame(A = c(1, 1, 2, 3), 
                       B = c("a", "b", "a", "c"), 
                       C = c("M", "N", "X", "M; N"))


DF %>% 
  group_by(A,B) %>% 
  summarise(C = paste(C, collapse = ";"))
#> # A tibble: 4 x 3
#> # Groups:   A [3]
#>       A B     C    
#>   <dbl> <fct> <chr>
#> 1     1 a     M    
#> 2     1 b     N    
#> 3     2 a     X    
#> 4     3 c     M;N
Related Posts Related Posts :
  • get first entries in rows of list?
  • Conditionally removing rows from a matrix in R
  • Using a loop to find P(-1.5<Y<1.5) for a range of sample sizes
  • R-Count and list the maximum count row by row
  • Include Iverson Bracket in R documentation
  • update a data frame and environment in R
  • How to write dynamic cumulative multiple in R
  • format time using as.POSIX in R
  • Change the class of multiple columns
  • Remove period and spaces within column headings nested in a list of data frames
  • R: error message --- package error: "functionName" not resolved from current namespace
  • labels with geom_text ggplot2
  • Passing mongodb ISODate in R
  • Importing "csv" file with multiple-character separator to R?
  • Change row names of a table obtained from a lm regression using xtable function
  • R language iterate over R object
  • How do you delete the header in a dataframe?
  • Re coding in R using complicated statement
  • accumulating functions and closures in R
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • Plotting coefficients and corresponding confidence intervals
  • passing expressions to curve() within a function
  • More effective merging of matched column with duplicates in data.table
  • Easy way to export multiple data.frame to multiple Excel worksheets
  • R Foreach Iterator - Walkforward
  • Table format and output in R
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org