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

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Thu Jul 28 10:31:38 CEST 2022


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)
> rbind(df1, df2)
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 using gmail.com> 
Sent: Thursday, July 28, 2022 9:57 AM
To: PIKAL Petr <petr.pikal using precheza.cz>; 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

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 using 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 using 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 using gmail.com> 
Sent: Wednesday, July 27, 2022 11:01 AM
To: PIKAL Petr <mailto:petr.pikal using precheza.cz>
Cc: R-help <mailto: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.

On Wed, Jul 27, 2022 at 1:13 PM PIKAL Petr <mailto:mailto:petr.pikal using 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 using gmail.com> 
Sent: Wednesday, July 27, 2022 8:35 AM
To: PIKAL Petr <mailto:mailto:petr.pikal using precheza.cz>
Cc: R-help <mailto:mailto: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

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 using precheza.cz> wrote:
Hi.

>From what you say, plain "rbind" could be used, if the columns in both sets
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

> -----Original Message-----
> From: R-help <mailto:mailto:r-help-bounces using r-project.org> On Behalf Of Ranjeet Kumar Jha
> Sent: Monday, July 25, 2022 3:03 PM
> To: R-help <mailto:mailto:r-help using r-project.org>
> Subject: [R] Need to insert various rows of data from a data frame after
> particular rows from another dataframe
> 
> Hello Everyone,
> 
> I have dataset in a particular format in "dacnet_yield_update till
2019.xlsx" file,
> where I need to insert the data of rows 2018-2019 and
> 2019-2020 for the districts those data are available in "Kharif crops
yield_18-
> 19.xlsx".  I need to insert these two rows of data belonging to every
district, if
> data is available in a later excel file, just after the particular crop
group data for
> the particular district.
> 
> I have put the data file in the given link.
> https://drive.google.com/drive/u/0/folders/1dNmGTI8_c9PK1QqmfIjnpbyzuiC
> XgxFC
> 
> Please help solving this problem.
> 
> Regards and Thanks,
> Ranjeet
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> mailto:mailto:R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
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 using 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 using 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 using 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 using gmail.com


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



More information about the R-help mailing list