[R] Need to insert various rows of data from a data frame after particular rows from another dataframe

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Wed Jul 27 17:41:56 CEST 2022


Ranjeet,

As others have said, you have not shown enough to get decent answers.

What you describe sounds quite routine and is the first step many have to do when gathering data from disparate sources that were done by different people without much consideration it has to follow some specific pattern.

Every data frame has umpteen columns with names in some order and containing data in each column that is of some type.

So if you want to combine them vertically using something like rbind() then you must make sure the two align exactly.

You could do a names(df1) and names(df2) to see what you have. If both do not have the same number of columns, you may want to either delete some in one or add to the other. If the names of some columns are not exactly the same, rename them. If the order is not the same, reorder them. This is fairly simple stuff you can do in base R or in something like dplyr and is a standard part of getting data into a form you want.

A tad more subtle is the need for conversions if the above is not working for you. If one file has text where the other has integers, then convert one or the other. If one is all uppercase and the other not, again, you need to make the same if it matters. For rbind() not immediately but for some kind of merge or join, definitely. If your data already is stored as a factor, be careful if the two do not line up with the same factors.

Again, without some details that show how the two are not already aligned, it is up to you to do the work, step by step, until they are.

Of course there are other approaches that can make this less painful. You can simply make a NEW df dynamically like this:

newdf <- data.frame(alpha=c(df1$colx, df2$coly), beta=c(df1$..., df2$...), ...)

This concatenates vectors/columns rather than the entire data.frame and makes some sense if you want to keep just a few columns and the dataframes are otherwise in quite a scrambled order.

And just for correction, when you say your data is in EXCEL, generally that does not mean a .CSV file but a .XSLX file and that would be opened and read another way.


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Ranjeet Kumar Jha
Sent: Wednesday, July 27, 2022 5:01 AM
To: PIKAL Petr <petr.pikal using precheza.cz>
Cc: R-help <r-help using r-project.org>
Subject: Re: [R] Need to insert various rows of data from a data frame after particular rows from another dataframe

yeah rbind works find bur dataframe 2 has different format and then rbind can be used with first dataframe. But here I am struggling to bring the 2nd dataframe in the same format as the 1st one.



More information about the R-help mailing list