0

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.

my_data <- data.frame(
crop = c(arhar, arhar, rice, moong),
season_name = rep(kharif, 4),
state_id= c(1204, 1205, 1204, 1206),
state_name= c(Asam,Bihar, Asam, Orrisa),
district_name= c(Jorhat, Patna, Naagaon, Puri),
district_id = c(15016, 16312, 15089, 17032),
year_id = c(2016, 2015, 2017, 2017),
yield= c(0.86, 1.23, 0.96, 1.05))

2nd excel sheet has same dataframe but I need to get the 2018 and 2019 yeardID data from here and insert after same district for same crop in a state.

`my_2nd_data <- data.frame(
crop = c(arhar, arhar, rice, moong),
season_name = rep(kharif, 4),
state_id= c(1204, 1205, 1204, 1206),
state_name= c(Asam,Bihar, Asam, Orrisa),
district_name= c(Jorhat, Patna, Naagaon, Puri),
district_id = c(15016, 16312, 15089, 17032),
year_id = c(2018, 2018, 2019, 2018),
yield= c(0.96, 1.16, 0.99, 1.12))`

So, the rows of 2018 and 2019 for a particular crop for from "my_2nd_data" I need to insert in first dataframe after row of same district under a state. This should look like the attached image.enter image description here

I have put the data file in the given link. https://drive.google.com/drive/u/0/folders/1dNmGTI8_c9PK1QqmfIjnpbyzuiCXgxFC

RJ34
  • 27
  • 7

1 Answers1

0

I would simply go with a rbind() followed by a sorting, considering df1 and df2 your 2 dataframes :

df3=rbind(df1,df2)
df3=df3[order(df3$district,df3$year),]
library(writexl)
write_xlsx(df3,"df3.xlsx")
Basti
  • 1,703
  • 3
  • 10
  • 25
  • @Basti...it throws error...Error in match.names(clabs, names(xi)) : names do not match previous names – RJ34 Jul 26 '22 at 12:23
  • In the example you gave, both dataframes had the same column names. Check that your dataframes have the same column names to make it work – Basti Jul 26 '22 at 12:29
  • Please check the files in the given link. Actually, I am having more issues before binding the rows. To come to final step there are data cleaning needs to be done, which I am unable to do because of not proper pattern of data. – RJ34 Jul 26 '22 at 13:03
  • You provided two examples dataframe and my answer is dedicated to. There are 12+ files in your link. You will need to format your dataframes. If you have issues, please open a new post because this is not the same question that was formerly asked – Basti Jul 26 '22 at 13:23