Need Help Putting Raw Data Into Long Format

Hi,

I tried to use the same codes that I've always used for creating long formats. However, I am confused as to what I am doing wrong. Can one of you awesome people please help me. Thanks in advance!

ERROR MESSAGE:


CODES

require(vegan)
require(tidyverse)
require(ggplot2)
phylum.dat <- structure(list(... #reconstruct dput
phylum.t1 <- decostand(phylum.dat, "total") # percent transformation on samples; “total” method defaults to rows (samples)
phylum.t2 <- decostand(phylum.t1, "max") # percent of maximum transformation on taxa; “max” method defaults to columns (taxa)
phylum.t2.dca <- decorana(phylum.t2) # default ira=0 specifies DCA
#put raw data into long format
phylum.dat_long <- phylum.dat %>%
mutate(Plot = rownames(.)) %>%
pivot_longer(cols = starts_with("p"),
names_to = "TaxGroup",
values_to = "Abundance")


DPUT

structure(list(p__Proteobacteria = c(44.807, 40.907, 36.558,
36.811, 39.401, 40.114, 45.911, 43.133, 30.137, 27.734, 26.722,
31.261), p__Actinobacteria = c(26.819, 34.651, 40.904, 38.847,
39.446, 37.523, 29.881, 29.251, 31.783, 23.641, 34.918, 31.308
), p__Acidobacteria = c(8.48, 6.6, 5.934, 6.609, 5.89, 7.567,
5.795, 6.666, 10.616, 10.709, 8.988, 11.794), p__Bacteroidetes = c(7.56,
8.189, 5.363, 6.223, 4.716, 3.613, 4.65, 5.2, 4.281, 2.785, 2.808,
3.271), p__Gemmatimonadetes = c(3.529, 2.108, 1.213, 1.193, 1.541,
1.439, 1.006, 1.171, 5.794, 4.107, 4.001, 2.747), p__Chloroflexi = c(2.686,
2.987, 2.979, 3.049, 4.128, 4.564, 5.304, 4.624, 3.669, 2.775,
4.534, 4.94), p__Bacteria_unclassified = c(2.38, 1.869, 1.579,
1.247, 2.3, 2.108, 1.36, 1.193, 3.126, 1.885, 2.987, 2.37), p__Firmicutes = c(0.998,
0.807, 2.76, 2.962, 0.866, 1.32, 1.651, 2.073, 1.099, 1.046,
1.3, 1.302), p__Verrucomicrobia = c(0.676, 0.404, 0.32, 0.35,
0.293, 0.239, 0.188, 0.261, 0.521, 0.726, 0.52, 0.397), p__Nitrospirae = c(0.464,
0.244, 0.198, 0.208, 0.016, 0.032, 0.024, 0.042, 0.296, 0.103,
0.229, 0.211), p__Candidatus_Saccharibacteria = c(0.421, 0.511,
0.456, 0.552, 0.523, 0.6, 0.842, 1.016, 0.672, 0.636, 0.465,
0.736), p__Planctomycetes = c(0.392, 0.267, 0.354, 0.285, 0.275,
0.356, 0.285, 0.276, 0.33, 0.438, 0.552, 0.365), p__Fibrobacteres = c(0.14,
0.074, 0.007, 0.009, 0.072, 0.044, 0.136, 0.079, 0.117, 0.018,
0.167, 0.065), p__Candidatus_Latescibacteria = c(0.113, 0.059,
0.017, 0.005, 0.004, 0.017, 0.015, 0.009, 0, 0.011, 0.007, 0.018
), p__Latescibacteria = c(0.085, 0.04, 0.01, 0.004, 0.012, 0.015,
0.033, 0.015, 0.012, 0.016, 0.011, 0.018), p__Cyanobacteria = c(0.079,
0.048, 1.071, 1.372, 0.32, 0.19, 2.629, 4.689, 7.133, 22.963,
11.417, 8.767), p__Thermodesulfobacteria = c(0.068, 0.057, 0.115,
0.103, 0.008, 0.01, 0.015, 0.007, 0.01, 0.003, 0.002, 0.013),
p__Elusimicrobia = c(0.059, 0.021, 0.012, 0.001, 0.004, 0.002,
0.015, 0.017, 0, 0.002, 0.005, 0.006), p__Chlorobi = c(0.052,
0.025, 0.002, 0.012, 0.029, 0.046, 0.033, 0.04, 0.05, 0.02,
0.046, 0.025), p__Armatimonadetes = c(0.046, 0.053, 0.051,
0.072, 0.076, 0.095, 0.048, 0.053, 0.197, 0.159, 0.128, 0.125
), p__Spirochaetes = c(0.035, 0.021, 0.002, 0.001, 0, 0.002,
0.024, 0.039, 0, 0, 0, 0), p__Parcubacteria = c(0.03, 0.013,
0, 0, 0.01, 0.015, 0.042, 0.037, 0.032, 0.059, 0.053, 0.011
), p__Chlamydiae = c(0.028, 0.017, 0.046, 0.05, 0.014, 0.007,
0.021, 0.022, 0.07, 0.074, 0.08, 0.152), p__Tenericutes = c(0.013,
0.008, 0.002, 0.004, 0.006, 0.005, 0.018, 0.033, 0.006, 0.003,
0.025, 0.046), p__BRC1 = c(0.011, 0.008, 0.002, 0.003, 0.008,
0.015, 0.006, 0.013, 0.018, 0.026, 0.016, 0.006), p__Thermotogae = c(0.007,
0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), p__candidate_division_WPS.1 = c(0.006,
0.004, 0.024, 0.011, 0.008, 0.005, 0.009, 0.006, 0.024, 0.033,
0.018, 0.023), p__Deinococcus.Thermus = c(0.004, 0, 0.002,
0.003, 0.002, 0.002, 0, 0.004, 0.002, 0.008, 0, 0.003), p__Ignavibacteriae = c(0.002,
0, 0.002, 0.003, 0.004, 0.012, 0.003, 0, 0, 0, 0.002, 0),
p__Candidatus_Gracilibacteria = c(0.002, 0, 0.005, 0.003,
0.002, 0, 0.003, 0, 0, 0, 0, 0.001), p__Candidatus_Microgenomates = c(0.002,
0, 0.005, 0, 0, 0, 0, 0, 0, 0.002, 0, 0), p__Candidatus_Melainabacteria = c(0.002,
0.002, 0, 0, 0, 0, 0, 0, 0, 0.002, 0, 0), p__Abditibacteriota = c(0.002,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), p__Candidatus_Giovannonibacteria = c(0.002,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), p__Candidatus_Tectomicrobia = c(0,
0, 0, 0, 0.014, 0.029, 0.024, 0.017, 0, 0, 0, 0), p__unclassified = c(0,
0, 0.002, 0, 0.008, 0.002, 0, 0, 0, 0.005, 0, 0.01), p__candidate_division_WWE3 = c(0,
0.002, 0, 0.003, 0, 0.002, 0.012, 0.002, 0, 0, 0, 0.003),
p__Microgenomates = c(0, 0.002, 0, 0.001, 0, 0.005, 0.003,
0.002, 0.002, 0.003, 0, 0.001), p__Fusobacteria = c(0, 0,
0, 0, 0, 0, 0.003, 0.004, 0, 0, 0, 0.001), p__candidate_division_WPS.2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0.003, 0, 0.003), p__Hydrogenedentes = c(0,
0, 0, 0, 0, 0.002, 0, 0.002, 0, 0.002, 0, 0), p__Candidatus_Hydrogenedentes = c(0,
0, 0, 0, 0, 0, 0, 0, 0.002, 0.002, 0, 0), p__Candidatus_Yanofskybacteria = c(0,
0, 0, 0, 0, 0, 0, 0, 0.002, 0.002, 0, 0), p__Aquificae = c(0,
0, 0, 0, 0, 0, 0.003, 0, 0, 0, 0, 0), p__Candidatus_Levybacteria = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0.002, 0, 0), p__Candidatus_Nomurabacteria = c(0,
0, 0, 0, 0, 0, 0, 0.002, 0, 0, 0, 0), p__Candidatus_Omnitrophica = c(0,
0, 0, 0, 0, 0, 0, 0.002, 0, 0, 0, 0), p__Candidatus_Woesebacteria = c(0,
0, 0, 0.001, 0, 0, 0, 0, 0, 0, 0, 0), p__Deferribacteres = c(0,
0, 0, 0, 0.002, 0, 0, 0, 0, 0, 0, 0), p__Lentisphaerae = c(0,
0, 0, 0, 0, 0, 0, 0.002, 0, 0, 0, 0), p__Synergistetes = c(0,
0, 0, 0, 0, 0, 0.003, 0, 0, 0, 0, 0), p__candidate_division_ZB3 = c(0,
0, 0, 0, 0, 0, 0.003, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c("D15B",
"D610B", "D15F", "D610F", "HR15B", "HR610B", "HR15F", "HR610F",
"C15B", "C610B", "C15F", "C610F"))

The problem with your code is simple. You created a dedicated column for your row names and you called it "Plot". Then, in your call to pivot_longer(), you selected all columns that start with "p". So, the Plot column was selected too (yes, even though "Plot" is with a capital "P"). What you need to do to be a bit more precise about the columns you want to pivot. Here is a solution:

phylum.dat %>%
  tibble::rownames_to_column(var = "Plot") %>% # Turn rownames into a column
  pivot_longer(cols = starts_with("p__"), names_to = "tax_group", values_to = "abundance")
1 Like

Awesome! One more thing. Since I am not familiar with tibble, is there any way I can transfer this to the environment so that I can view it in its entirety as a data set?

In R, if your dataset is too big, it will be truncated in the console so you will not be able to view it entirely. You can; however, use the View() function (with a capital "V") for that.

If we assume that your dataset is stored in a variable my_data, you can run View(my_data). This will open a new tab where your data will be displayed in tabular format. You'll then be able to explore it in its entirety.

1 Like

Thank you gueyenono for your help

I realized that I could create the table by using an assignment operator:

phylum.dat_long <- phylum.dat %>%
tibble::rownames_to_column(var = "Plot") %>% # Turn rownames into a column
pivot_longer(cols = starts_with("p__"), names_to = "tax_group", values_to = "abundance")

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.