-1

Here is my example data which was in a csv and imported in R.

Rowid   parcel no           crop    area    Area insured
1             122         cotton     0.9            1.2
2             111           soya     0.8            1.1
3             111         cotton     1.2            1.6
4              23           soya     0.7            1.5
5              45         cotton    0.23            1.3
6              45           soya     1.6            1.0

I would like to arrange it as

parcel no       crop    area    Area insured     crop   area.   Area insured
122           cotton     0.9            1.2         
111             soya     0.8            1.1      cotton 1.2      1.6
23              soya     0.7            1.5         
45            cotton    0.23            1.3      soya   1.6      .01

I am wandering how can I arrive on above mentioned results. Is there a possible way to do this in R? I have a very bulky data to be arranged in this mannerenter image description here

Gopal Krishna
  • 55
  • 1
  • 2
  • 9
  • I tried to format your data for you, since there was no formatting applied (writing/editing a question gives you the option to format as code, which you should do), but can't actually tell how these columns are supposed to be lined up. What's the logic for going from one version to the next? – camille Feb 28 '19 at 17:10
  • Yes I am aware of the code formating but I was not aware of the data arrangement. This has nothing to do with the research impact of the question. Which u have marked as negative. – Gopal Krishna Feb 28 '19 at 17:52
  • The logic is that the parcel no. are same in row no. 3 and 6. I would like to arrange data according to those parcel no. Which are common for cotton and soya crop as given in example output. – Gopal Krishna Feb 28 '19 at 17:55
  • Sure, the formatting isn't a huge deal. But your question didn't include a clear explanation of what you're trying to do, how you're trying to get from one step to the next (trying to create wide-shaped data, but without additional column names...?), or code you've written so far that hasn't worked. Take a look at writing a [reproducible R example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Feb 28 '19 at 18:21

1 Answers1

1

One way to solve this is to extract the rows with duplicated parcel and then merge. I do this for a minimal example:

df<-data.frame(c(11,11,12,13,13),c("a","b","c","d","a"))
names(df)<-c("parcel","crop")
df1<-df[duplicated(df$parcel),]
df2<-df[!duplicated(df$parcel),]
merge(df2,df1,by="parcel",all.x=T,all.y=T)

You get as output

  parcel crop.x crop.y
1     11      a      b
2     12      c   <NA>
3     13      d      a

where the NA represents an empty cell in your example. Does this solve your problem?

tarion
  • 48
  • 8
  • Thanx @tarion. This a good way to get the arranged data as per one column. I applied this approach and got rows aligned with parcel no. Column. Now I m looking to sort the data as per my requirement. Thanks again. – Gopal Krishna Mar 02 '19 at 14:29
  • You're welcome. Consider accepting the answer (ticking the check mark symbol) if you find it helpful. And/or you might consider following camille's advice to construct a reproducible example to get more answers. – tarion Mar 02 '19 at 16:34