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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet
Need to insert various rows of data from a data frame after particular rows from another dataframe
17 messages · Ranjeet Kumar Jha, Calum Polwart, PIKAL Petr +3 more
1 day later
Not very clear what you are trying to do. But I'd have thought possibly dplyr left_join might be a solution for you. The base R equivalent is merge(). It might be a rbind or cbind can do it too. On Wed, 27 Jul 2022, 03:30 Ranjeet Kumar Jha, <ranjeetjhaiitkgp at gmail.com> wrote:
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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at 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.
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 <r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 3:03 PM To: R-help <r-help at 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]]
______________________________________________ R-help at 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.
Hi Calum, Thanks for your reply ! Actually, I need to bring the 2018-2019 and 2019-2020 yield data corresponding to each crop in every district under a state from the "Kharif crop yield 18-19.xls" into "dacnet" file, given in the link. These two years of data from Kharif file should be brought into dacnet file after 2017 or whichever latest previous year yield that crop contains for each district in a particular state. Since Kharif file has different format and 11-12 crops with large number of districts in each state, I am unable to get the data from Kharif to dacnet into same format. So basically, kharif file yield data for 2018-2019 needs to be brought into dacnet file in the same format as it is in dacnet file. I hope it is clear now. I will really appreciate your help! Regards and Thanks,
On Wed, Jul 27, 2022 at 11:52 AM CALUM POLWART <polc1410 at gmail.com> wrote:
Not very clear what you are trying to do. But I'd have thought possibly dplyr left_join might be a solution for you. The base R equivalent is merge(). It might be a rbind or cbind can do it too. On Wed, 27 Jul 2022, 03:30 Ranjeet Kumar Jha, <ranjeetjhaiitkgp at gmail.com> wrote:
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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at 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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
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 <petr.pikal at 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 <r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar
Jha
Sent: Monday, July 25, 2022 3:03 PM To: R-help <r-help at 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]]
______________________________________________ R-help at 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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
I don't open links to random data files. Sorry. It sounds like you need to subset or filter your later data to get what you want and then merge it in. Good luck finding someone who will open a random excel sheet and then write your code for you On Wed, 27 Jul 2022, 07:32 Ranjeet Kumar Jha, <ranjeetjhaiitkgp at gmail.com> wrote:
Hi Calum, Thanks for your reply ! Actually, I need to bring the 2018-2019 and 2019-2020 yield data corresponding to each crop in every district under a state from the "Kharif crop yield 18-19.xls" into "dacnet" file, given in the link. These two years of data from Kharif file should be brought into dacnet file after 2017 or whichever latest previous year yield that crop contains for each district in a particular state. Since Kharif file has different format and 11-12 crops with large number of districts in each state, I am unable to get the data from Kharif to dacnet into same format. So basically, kharif file yield data for 2018-2019 needs to be brought into dacnet file in the same format as it is in dacnet file. I hope it is clear now. I will really appreciate your help! Regards and Thanks, On Wed, Jul 27, 2022 at 11:52 AM CALUM POLWART <polc1410 at gmail.com> wrote:
Not very clear what you are trying to do. But I'd have thought possibly dplyr left_join might be a solution for you. The base R equivalent is merge(). It might be a rbind or cbind can do it too. On Wed, 27 Jul 2022, 03:30 Ranjeet Kumar Jha, <ranjeetjhaiitkgp at gmail.com> wrote:
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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at 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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"*
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 <ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 8:35 AM
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: 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
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 <petr.pikal at precheza.cz <mailto:petr.pikal at 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 <r-help-bounces at r-project.org <mailto:r-help-bounces at r-project.org> > On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 3:03 PM To: R-help <r-help at r-project.org <mailto:r-help at 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]]
______________________________________________ R-help at r-project.org <mailto:R-help at 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:ranjeetjhaiitkgp at gmail.com> ranjeetjhaiitkgp at gmail.com "Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"
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 <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 <ranjeetjhaiitkgp at gmail.com>
*Sent:* Wednesday, July 27, 2022 8:35 AM
*To:* PIKAL Petr <petr.pikal at precheza.cz>
*Cc:* 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
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 <petr.pikal at 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 <r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar
Jha
Sent: Monday, July 25, 2022 3:03 PM To: R-help <r-help at 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]]
______________________________________________ R-help at 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: *ranjeetjhaiitkgp at gmail.com <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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
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 <ranjeetjhaiitkgp at gmail.com> Sent: Wednesday, July 27, 2022 11:01 AM To: PIKAL Petr <petr.pikal at precheza.cz> Cc: 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 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: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:ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 8:35 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
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:petr.pikal at 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:r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 3:03 PM To: R-help <mailto:r-help at 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:R-help at 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: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!"
I think what you want is full_join() from the dplyr package. https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/join The only requirement is that both data frames must have a column in common wherein the data are entered in the same way. So the column labeled "state" needs to have "ANANTAPUR" in both data frames rather than "ANANTAPUR" in one data frame and "Anantapur" in the other. Reformat your excel spreadsheet to remove headers. Your first column should be three columns: State, then Crop, then district rather than headings. The first row can contain variable names. I would make variable names simple (like "Area" and "Production") but some like more information so "Area_Ha" and "Production_TN_per_Ha" would also work. It is best not to use special symbols in variable names and keep variable names as one string of characters (no spaces). Including such will eventually cause problems. https://www.w3schools.com/r/r_variables.asp#:~:text=Variable%20Names&text=Rules%20for%20R%20variables%20are,be%20followed%20by%20a%20digit. One exception to the rules in the link is that you can make a variable name T or F. R defaults to interpreting these as TRUE and FALSE. The problem happens when the programmer reassigns these and then tries to use T or F as Boolean in other parts of the program. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 9:03 AM To: R-help <r-help at r-project.org> Subject: [R] Need to insert various rows of data from a data frame after particular rows from another dataframe [External Email] 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://urldefense.proofpoint.com/v2/url?u=https-3A__drive.google.com_drive_u_0_folders_1dNmGTI8-5Fc9PK1QqmfIjnpbyzuiCXgxFC&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=4_bhl2_drIA0Pn3LHMcoAd02lX0t6bAx2wSlhVAJelA&e= Please help solving this problem. Regards and Thanks, Ranjeet ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=MAGsb78RBOWV0usgeNnmHsZcYoQI959dmihJ9Ycs8Lo&e= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=PSiyw67xhInkZo69l1HojQKGOqthbxYpGL5Q14cPo8w&e= and provide commented, minimal, self-contained, reproducible code.
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 at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Wednesday, July 27, 2022 5:01 AM To: PIKAL Petr <petr.pikal at precheza.cz> Cc: 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 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.
I'm retired, and I had an hour on my hands while tea cooked and my
granddaughter did her homework, and I just *love* showing off how helpful I
am.
Good news: someone finally looked at your data.
(That would be me.)
Bad news: it's going to be a lot of work to do what you want to, and YOU
SHOULDN'T EVEN TRY because it won't make any sense, as we would all have
known at once had you been clear about the structure of your data in the
beginning.
You have two files.
dacnet_yield_update till 2019.csv
is a straightforward "data" file with structure
crop factor(arhar,bajra,cotton,gram,maize,moong,mustard,
potato,rice,soyabean,sugarcane,urad,wheat)
season factor(kharif,rabi),
state.id integer(1201..1235),
state.name factor, # 34 states
district.id integer(15001..15648),
district.name factor,
year integer(1998..2017),
yield decimal(0.001 .. 314.736, precision=3)
The one problem is that the file name is misleading. It says "till 2019",
but includes no data for 2019 or 2018.
Ah, but the other file! That's not a "data" file intended for machine use
at all. It's a "display" file intended for human beings to look at and go
"wow, gosh, lookit them numbahs". It's the kind of thing that gets
included as an appendix in an official report which seems as if perversely
designed to impede the development of insight as much as possible.
Amongst other difficulties:
- the same column contains state names, district names, crop names, and
assorted junk;
- state names are not coded the same way in the two files;
- district names are in UPPERCASE in the .xls files and have numbers
prefixed to them for no apparent reason;
- crop names are not coded the same way in the two files;
- yields are not coded the same way in the two files (3 digit precision in
one, 2 digit precision in the other) and I have some doubt as to whether
they are measuring the same thing;
- above all, years appear to be CALENDAR years in the .csv file (e.g.,
2017) but FINANCIAL years in the .xsl file (e.g., 2018-19)
Now I could wrangle the .xls file into something closer to the .csv file
easily enough. I'd do it by converting the .xsl to .csv, then writing a
script in AWK. *BUT* my uncertainty that "yield" means the same thing in
the two files and my certainty that "year" does NOT mean the same thing
make it unrewarding to do so.
The .xls file is the end product of some process that derived it from data
better structured for computation. It seems like a better use of your time
to go and look for the original data.
It also seems like a good use of your time to make certain you know what
the fields of the .csv file actually mean. ARE those calendar yields, or
just part of a financial year? Why are only the yields of interest and not
the area planted? How are the yields computed?
On Wed, 27 Jul 2022 at 14:31, Ranjeet Kumar Jha <ranjeetjhaiitkgp at gmail.com>
wrote:
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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at 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.
Thank you very much Sir! You please don't do that much hard work now. I have made life easy for you. I have formatted the 2nd file "kharif_18_19" in the same format as it is in the dacnet file. Now, both files I want as combined where in dacnet file 2018 and 2019 yield data for each crop for every district and state should appear just below the row of previous years of data.
On Thu, Jul 28, 2022 at 12:11 PM Richard O'Keefe <raoknz at gmail.com> wrote:
I'm retired, and I had an hour on my hands while tea cooked and my
granddaughter did her homework, and I just *love* showing off how helpful I
am.
Good news: someone finally looked at your data.
(That would be me.)
Bad news: it's going to be a lot of work to do what you want to, and YOU
SHOULDN'T EVEN TRY because it won't make any sense, as we would all have
known at once had you been clear about the structure of your data in the
beginning.
You have two files.
dacnet_yield_update till 2019.csv
is a straightforward "data" file with structure
crop factor(arhar,bajra,cotton,gram,maize,moong,mustard,
potato,rice,soyabean,sugarcane,urad,wheat)
season factor(kharif,rabi),
state.id integer(1201..1235),
state.name factor, # 34 states
district.id integer(15001..15648),
district.name factor,
year integer(1998..2017),
yield decimal(0.001 .. 314.736, precision=3)
The one problem is that the file name is misleading. It says "till 2019",
but includes no data for 2019 or 2018.
Ah, but the other file! That's not a "data" file intended for machine use
at all. It's a "display" file intended for human beings to look at and go
"wow, gosh, lookit them numbahs". It's the kind of thing that gets
included as an appendix in an official report which seems as if perversely
designed to impede the development of insight as much as possible.
Amongst other difficulties:
- the same column contains state names, district names, crop names, and
assorted junk;
- state names are not coded the same way in the two files;
- district names are in UPPERCASE in the .xls files and have numbers
prefixed to them for no apparent reason;
- crop names are not coded the same way in the two files;
- yields are not coded the same way in the two files (3 digit precision in
one, 2 digit precision in the other) and I have some doubt as to whether
they are measuring the same thing;
- above all, years appear to be CALENDAR years in the .csv file (e.g.,
2017) but FINANCIAL years in the .xsl file (e.g., 2018-19)
Now I could wrangle the .xls file into something closer to the .csv file
easily enough. I'd do it by converting the .xsl to .csv, then writing a
script in AWK. *BUT* my uncertainty that "yield" means the same thing in
the two files and my certainty that "year" does NOT mean the same thing
make it unrewarding to do so.
The .xls file is the end product of some process that derived it from data
better structured for computation. It seems like a better use of your time
to go and look for the original data.
It also seems like a good use of your time to make certain you know what
the fields of the .csv file actually mean. ARE those calendar yields, or
just part of a financial year? Why are only the yields of interest and not
the area planted? How are the yields computed?
On Wed, 27 Jul 2022 at 14:31, Ranjeet Kumar Jha <
ranjeetjhaiitkgp at gmail.com> wrote:
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_c9PK1QqmfIjnpbyzuiCXgxFC Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at 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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
I tried using the merge, and join function but it is not providing in the rows below the yield data of previous years for the same crop in the dacnet file.
On Wed, Jul 27, 2022 at 6:04 PM Ebert,Timothy Aaron <tebert at ufl.edu> wrote:
I think what you want is full_join() from the dplyr package. https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/join The only requirement is that both data frames must have a column in common wherein the data are entered in the same way. So the column labeled "state" needs to have "ANANTAPUR" in both data frames rather than "ANANTAPUR" in one data frame and "Anantapur" in the other. Reformat your excel spreadsheet to remove headers. Your first column should be three columns: State, then Crop, then district rather than headings. The first row can contain variable names. I would make variable names simple (like "Area" and "Production") but some like more information so "Area_Ha" and "Production_TN_per_Ha" would also work. It is best not to use special symbols in variable names and keep variable names as one string of characters (no spaces). Including such will eventually cause problems. https://www.w3schools.com/r/r_variables.asp#:~:text=Variable%20Names&text=Rules%20for%20R%20variables%20are,be%20followed%20by%20a%20digit . One exception to the rules in the link is that you can make a variable name T or F. R defaults to interpreting these as TRUE and FALSE. The problem happens when the programmer reassigns these and then tries to use T or F as Boolean in other parts of the program. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 9:03 AM To: R-help <r-help at r-project.org> Subject: [R] Need to insert various rows of data from a data frame after particular rows from another dataframe [External Email] 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://urldefense.proofpoint.com/v2/url?u=https-3A__drive.google.com_drive_u_0_folders_1dNmGTI8-5Fc9PK1QqmfIjnpbyzuiCXgxFC&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=4_bhl2_drIA0Pn3LHMcoAd02lX0t6bAx2wSlhVAJelA&e= Please help solving this problem. Regards and Thanks, Ranjeet [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=MAGsb78RBOWV0usgeNnmHsZcYoQI959dmihJ9Ycs8Lo&e= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=9B32l682GguXDLEFdPm6j5JZNatveGSlY7lnwLYFVOW2TX1tNLeHbDE49MYxSh_Q&s=PSiyw67xhInkZo69l1HojQKGOqthbxYpGL5Q14cPo8w&e= 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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
Hi Pikal, Now I have formatted the kharif data file in the same format as it is in the dacnet filecode:. However, "district.id" and "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 <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 <ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 11:01 AM
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: 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
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: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:ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 8:35 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
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:petr.pikal at 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:r-help-bounces at r-project.org> On Behalf Of Ranjeet
Kumar Jha
Sent: Monday, July 25, 2022 3:03 PM To: R-help <mailto:r-help at 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:R-help at 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: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!"
Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA) https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56 ----------------------------------------------------------- Email: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
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 <
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, "district.id" and "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 <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 <ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 11:01 AM
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: 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
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: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:ranjeetjhaiitkgp at gmail.com>
Sent: Wednesday, July 27, 2022 8:35 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
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:
petr.pikal at 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:r-help-bounces at r-project.org> On Behalf Of
Ranjeet Kumar Jha
Sent: Monday, July 25, 2022 3:03 PM To: R-help <mailto:r-help at 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:R-help at 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: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!"
-- Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA) https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56 ----------------------------------------------------------- Email: *ranjeetjhaiitkgp at gmail.com <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: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>* *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our lives beautiful!"* [[alternative HTML version deleted]]
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 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.
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 at r-project.org> On Behalf Of Ranjeet Kumar Jha Sent: Monday, July 25, 2022 3:03 PM To: R-help <mailto:mailto:r-help at 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 at 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 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!"
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!"