Why is pivot_longer is creating duplicate rows in my data frame?
I am using pivot_longer to clean up my data frame and it created three duplicate rows for each plant species.
Here is a sample of the data frame:
latin_name plant_type treatment `ounces per plot` weight `grams per plot`
<chr> <chr> <chr> <dbl> <chr> <dbl>
1 Agastache foeniculum wildflower tx._one_rate 0.021 wt_per_plot_one 0.609
2 Agastache foeniculum wildflower tx._one_rate 0.021 wt_per_plot_two 1.22
3 Agastache foeniculum wildflower tx._one_rate 0.021 wt_per_plot_three 1.83
4 Agastache foeniculum wildflower tx._one_rate 0.021 wt_per_plot_four 2.44
5 Agastache foeniculum wildflower tx._two._rate 0.0430 wt_per_plot_one 0.609
6 Agastache foeniculum wildflower tx._two._rate 0.0430 wt_per_plot_two 1.22
7 Agastache foeniculum wildflower tx._two._rate 0.0430 wt_per_plot_three 1.83
8 Agastache foeniculum wildflower tx._two._rate 0.0430 wt_per_plot_four 2.44
9 Agastache foeniculum wildflower tx._three._rate 0.0645 wt_per_plot_one 0.609
10 Agastache foeniculum wildflower tx._three._rate 0.0645 wt_per_plot_two 1.22
And here is the code chunk I used:
library(tidyverse)
seed_rate1 <- seed_rate1%>%
pivot_longer(
cols = starts_with("tx"),
names_to = "treatment",
values_to = "ounces per plot")%>%
pivot_longer(
cols = starts_with("wt"),
names_to = "weight",
values_to = "grams per plot")
And here is the first 12 rows and all columns using dput():
seed_struct <-
structure(structure(
list(
latin_name = c(
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum",
"Agastache foeniculum"
),
plant_type = c(
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower",
"wildflower"
),
treatment = c(
"tx._one_rate",
"tx._one_rate",
"tx._one_rate",
"tx._one_rate",
"tx._two._rate",
"tx._two._rate",
"tx._two._rate",
"tx._two._rate",
"tx._three._rate",
"tx._three._rate",
"tx._three._rate",
"tx._three._rate"
),
ounces per plot
= c(
0.021,
0.021,
0.021,
0.021,
0.042975207,
0.042975207,
0.042975207,
0.042975207,
0.06446281,
0.06446281,
0.06446281,
0.06446281
),
weight = c(
"wt_per_plot_one",
"wt_per_plot_two",
"wt_per_plot_three",
"wt_per_plot_four",
"wt_per_plot_one",
"wt_per_plot_two",
"wt_per_plot_three",
"wt_per_plot_four",
"wt_per_plot_one",
"wt_per_plot_two",
"wt_per_plot_three",
"wt_per_plot_four"
),
grams per plot
= c(
0.609,
1.218,
1.827,
2.437,
0.609,
1.218,
1.827,
2.437,
0.609,
1.218,
1.827,
2.437
)
),
row.names = c(NA,-12L),
class = c("tbl_df", "tbl", "data.frame")
))
Unable to figure out how to fix this i tried to use distinct() with no luck either...I am new to R so I suspect there is something in the pivot_longer that I am not doing correctly. If anyone has any input I would greatly appreciate. I am new to R so very specific/ easy to understand responses would be much appreciated!