Skip to content
Prev 392362 / 398502 Next

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

Hallo Ranjeet

You got some other answers which revealed that the original data were problematic. The error suggests that you have different number of columns in each data frame.  

df1 <- data.frame(a=letters[1:5], b=1:5)
df2 <- data.frame(a=LETTERS[1:5], b=letters[1:5], c=1:5)
Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

So for rbind to work, both data frames should have the same number of columns and also the correct column in correct place. If it is not the case, you could use merge, but the missing values in common column, (in this case b) will be filled by NA.

df2 <- data.frame(a=LETTERS[1:10], b=letters[1:10], c=1:10)
merge(df1, df2, by.x="a", by.y="b", all=TRUE)
   a  b a.y  c
1  a  1   A  1
2  b  2   B  2
3  c  3   C  3
4  d  4   D  4
5  e  5   E  5
6  f NA   F  6
7  g NA   G  7
8  h NA   H  8
9  i NA   I  9
10 j NA   J 10
If you managed to get both, kharif and dacnet into R it would be worthwhile to show at least the structure of your data by sending result of 

str(kharif_18_19) and str(dacnet_17)
Structure should be the same.

Or better to send a chunk of the data as output from

dput(head(kharif_18_19),20) and dput(head(dacnet_17),20)

so everybody could inspect directly how those objects in your R look like.

Cheers
Petr

And BTW, do not use HTML formating, it could scramble your email as r-help list is plain text only.

From: Ranjeet Kumar Jha <ranjeetjhaiitkgp at gmail.com> 
Sent: Thursday, July 28, 2022 9:57 AM
To: PIKAL Petr <petr.pikal at precheza.cz>; R-help <r-help at r-project.org>
Subject: Re: [R] Need to insert various rows of data from a data frame after particular rows from another dataframe

To check the error in columns when I use "intersection", it returns all the 8 columns. That means no difference in columns.
On Thu, Jul 28, 2022 at 1:12 PM Ranjeet Kumar Jha <mailto:ranjeetjhaiitkgp at gmail.com> wrote:
Hi Pikal,

Now I have formatted the kharif data file in the same format as it is in the dacnet filecode:. However, "http://district.noclick_id" and "http://state.id " columns have "N/A" data that are appearing. If I use merge "rbind" function, it still throws this error:  

"Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match"

code:
df3=rbind(kharif_18_19,dacnet_17)
df3=df3[order(df3$district,df3$year),]
x<-write.csv(df3,"df3.csv")
view(x)
On Wed, Jul 27, 2022 at 3:18 PM PIKAL Petr <mailto:petr.pikal at precheza.cz> wrote:
Hallo, 

I do not understand what you really want and you do not help much. 

No error, no data, vague description of your problem, no effort to explain it better. Only you can see your data, only you can see the error message so we are clueless.

Cheers
Petr


From: Ranjeet Kumar Jha <mailto:ranjeetjhaiitkgp at gmail.com> 
Sent: Wednesday, July 27, 2022 11:01 AM
To: PIKAL Petr <mailto:petr.pikal at precheza.cz>
Cc: R-help <mailto:r-help at 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.
On Wed, Jul 27, 2022 at 1:13 PM PIKAL Petr <mailto:mailto:petr.pikal at precheza.cz> wrote:
Hi.

?is not working? is extremelly vague.

1.
What do you expect this code do?

for(cr in seq_along(dacnet_17$district)){
    match(arhar_18$district, dacnet_17$district)
}

See ?match and maybe also ??for? and try this

x <- letters[1:5]
y <- sample(letters, 100, replace=T)
match(x,y)
[1] 45 16 24 13 71
for(i in 1:3) match(x,y)

2.
rbind works as expeceted

arhar_18 <- data.frame(a=1:10, b=50, c=letters[1:10])
dacnet_17 <- data.frame(a=11:20, b=100, c=sample(letters,10))
df3=rbind(arhar_18,dacnet_17)

if your data has common column order and type.

To get more specific answer you need to ask specific question preferably with some data included (most preferably by dput command) and error message.

Cheers
Petr


From: Ranjeet Kumar Jha <mailto:mailto:ranjeetjhaiitkgp at gmail.com> 
Sent: Wednesday, July 27, 2022 8:35 AM
To: PIKAL Petr <mailto:mailto:petr.pikal at precheza.cz>
Cc: R-help <mailto:mailto:r-help at r-project.org>
Subject: Re: [R] Need to insert various rows of data from a data frame after particular rows from another dataframe

Hi Petr,

I used r-bind but it's not working.
Here is the code:

arhar_18<-read.csv("D:/Ranjeet/IAMV6/input/yield/kharif_18-19_yield/Kharif_2018/arhar_18.csv")
dacnet_17<-read.csv("D:/Ranjeet/IAMV6/input/yield/dacnet_yield_update till 2019.csv")

for(cr in seq_along(dacnet_17$district)){
    match(arhar_18$district, dacnet_17$district)
}

df3=rbind(arhar_18,dacnet_17)
df3=df3[order(df3$district,df3$year),]
x<-write.csv(df3,"df3.csv")
view(x)
On Wed, Jul 27, 2022 at 12:00 PM PIKAL Petr <mailto:mailto:petr.pikal at precheza.cz> wrote:
Hi.
are the same and in the same order. After that you can reorder the resulting
data frame as you wish by "order". AFAIK for most functions row order in
data frame does not matter.

Cheers
Petr
2019.xlsx" file,
yield_18-
district, if
group data for
http://www.R-project.org/posting-guide.html
-- 
Ranjeet  Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA)
https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56
-----------------------------------------------------------
Email: mailto:mailto:ranjeetjhaiitkgp at gmail.com


"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"




-- 
Ranjeet  Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA)
https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56
-----------------------------------------------------------
Email: mailto:mailto:ranjeetjhaiitkgp at gmail.com


"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"



-- 
Ranjeet  Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA)
https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56
-----------------------------------------------------------
Email: mailto:ranjeetjhaiitkgp at gmail.com


"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"

Thread (17 messages)

Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 25 Calum Polwart Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 26 PIKAL Petr Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 26 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 26 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 26 Calum Polwart Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 26 PIKAL Petr Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 PIKAL Petr Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 Ebert,Timothy Aaron Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 @vi@e@gross m@iii@g oii gm@ii@com Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 Richard O'Keefe Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 27 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 28 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 28 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 28 Ranjeet Kumar Jha Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 28 PIKAL Petr Need to insert various rows of data from a data frame after particular rows from another dataframe Jul 28