0

I am working with a dataset from a herbicide experiment involving different herbicides and their ratings. I'm trying to use tidyr to manipulate the data. Here's what I'm working with:

library(readr)

library(tidyr)

library(dplyr)


plot <- c("101","102","103","104")
herbicide <- c("mesotrione","tembotrione","mesotrione","tembotrione")
plant_1 <- c("100","100","95","95")
plant_2 <- c("100","100","95","20")
plant_3 <- c("50","50","50","50")
plant_4 <- c("40","40","30","30")

dataframe1<- as.data.frame(cbind(plot, herbicide, plant_1, plant_2, plant_3,
plant_4))

I want to manipulate this data so that I will have the data sorted in columns as follows:

plot <-
c("101","101","101","101","102","102","102","102","103","103","103","103","104","104","104","104")

herbicide <-
c("mesotrione","mesotrione","mesotrione","mesotrione","tembotrione","tembotrione","tembotrione","tembotrione",              
"mesotrione","mesotrione","mesotrione","mesotrione","tembotrione","tembotrione","tembotrione","tembotrione")

plant <- c("1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4")

score <-
c("100","100","95","95","100","100","95","20","50","50","50","50","40","40","30","30")

dataframe2 <- as.data.frame(cbind(plot, herbicide, plant, score))

This is what I have tried, but it doesn't work for me:

sort <- gather(data1, plot, plant_score, herbicide)

Any help would be greatly appreciated, thanks everyone!

Shree
  • 10,835
  • 1
  • 14
  • 36
ihb
  • 292
  • 9
  • 27

1 Answers1

1

You are almost there with gather!

gather(dataframe1, plant, plant_score, -plot, -herbicide) %>% arrange(plot)

library(tidyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union


plot <- c("101","102","103","104")
herbicide <- c("mesotrione","tembotrione","mesotrione","tembotrione")
plant_1 <- c("100","100","95","95")
plant_2 <- c("100","100","95","20")
plant_3 <- c("50","50","50","50")
plant_4 <- c("40","40","30","30")

dataframe1<- as.data.frame(cbind(plot, herbicide, plant_1, plant_2, plant_3,
plant_4))

dataframe1
#>   plot   herbicide plant_1 plant_2 plant_3 plant_4
#> 1  101  mesotrione     100     100      50      40
#> 2  102 tembotrione     100     100      50      40
#> 3  103  mesotrione      95      95      50      30
#> 4  104 tembotrione      95      20      50      30
gather(dataframe1, plant, plant_score, -plot, -herbicide) %>% arrange(plot)
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
#>    plot   herbicide   plant plant_score
#> 1   101  mesotrione plant_1         100
#> 2   101  mesotrione plant_2         100
#> 3   101  mesotrione plant_3          50
#> 4   101  mesotrione plant_4          40
#> 5   102 tembotrione plant_1         100
#> 6   102 tembotrione plant_2         100
#> 7   102 tembotrione plant_3          50
#> 8   102 tembotrione plant_4          40
#> 9   103  mesotrione plant_1          95
#> 10  103  mesotrione plant_2          95
#> 11  103  mesotrione plant_3          50
#> 12  103  mesotrione plant_4          30
#> 13  104 tembotrione plant_1          95
#> 14  104 tembotrione plant_2          20
#> 15  104 tembotrione plant_3          50
#> 16  104 tembotrione plant_4          30