1

I'm just a beginner with R and I've been having some trouble organising my data set.

I have a particular problem. I'm working with a data set from an agricultural survey. In this data set, subjects (farms) have different plots and in those plots have different crops. Something like this

Subject CropName Area
A Corn 2
A Carrots 2
A Avocado 0,5
A Corn 1
B Grapes 2
C Carrots 3

As you can see, not all farms have the same crops and the same crops can be found in different plots of the same farm.

What I want to do is to sum up the values of each crop in a farm and separate them in different columns, to obtain only one observation per farm. Which will make my life easier on the analysis phase.

Something like this:

Subject Corn Carrots Avocado Grapes
A 3 2 0.5 0
B 0 0 0 2
C 0 3 0 0

I've have not idea how to do it. I'm open to suggestions about how my code should look like. Thanks a lot!

fabsp
  • 11
  • 1

1 Answers1

0

It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from dput(head(dataObject))), what you have tried so far, and any libraries you are using. Check it out: making R reproducible questions.

You can create this table, like so:

library(tidyverse)

df1 <- data.frame(
  stringsAsFactors = FALSE,
  Subject = c("A", "A", "A", "A", "B", "C"),
  CropName = c("Corn","Carrots",
               "Avocado","Corn","Grapes","Carrots"),
  Area = c("2", "2", "0.5", "1", "2", "3"))

head(df1)
#   Subject CropName Area
# 1       A     Corn    2
# 2       A  Carrots    2
# 3       A  Avocado  0.5
# 4       A     Corn    1
# 5       B   Grapes    2
# 6       C  Carrots    3 

df1 %>% mutate(Area = as.numeric(Area)) %>% # change to number
  group_by(CropName, Subject) %>%  # group by what's wanted as names
  summarise(Area = sum(Area)) %>%  # where the values will come from
  spread(CropName, Area, fill = 0) # render the spread, fill with 0

The console output will render the following:

# A tibble: 3 × 5
  Subject Avocado Carrots  Corn Grapes
  <chr>     <dbl>   <dbl> <dbl>  <dbl>
1 A           0.5       2     3      0
2 B           0         0     0      2
3 C           0         3     0      0 
Kat
  • 15,669
  • 3
  • 18
  • 51