简体   繁体   English

在 R 中使用 Tidyverse 分离数据时遇到问题

[英]Having trouble separating data using Tidyverse in R

I have a dataset consisting of 20 plant genotypes with measurements of LAI, V1, V2, V3, V4, V5 being taken at three growth stages (1, 2, 3).我有一个由 20 个植物基因型组成的数据集,在三个生长阶段(1、2、3)测量 LAI、V1、V2、V3、V4、V5。

I need to separate the data in R (using the tidyverse package) into columns of genotype, stage, and mesurement (consisting of LAI:V5).我需要将 R(使用 tidyverse 包)中的数据分成基因型、阶段和测量列(由 LAI:V5 组成)。 The code that I have tried does not work;我尝试过的代码不起作用; how could I go about doing this?我怎么能 go 这样做呢? Here is what I have tried:这是我尝试过的:

#Open packages
library(readr)
library(tidyr)
library(dplyr)

#Dataset:
dataset <- structure(list(plot = c(101, 102, 103, 104, 105, 106, 107, 108, 
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 101, 
102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 
115, 116, 117, 118, 119, 120, 101, 102, 103, 104, 105, 106, 107, 
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120
), genotype = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20), stage = c(1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), LAI = c(822, 763, 
551, 251, 800, 761, 343, 593, 997, 261, 19, 429, 566, 574, 174, 
356, 891, 918, 948, 782, 902, 383, 704, 157, 358, 453, 723, 644, 
308, 149, 504, 437, 348, 165, 128, 305, 778, 516, 347, 212, 792, 
423, 565, 828, 106, 605, 603, 551, 145, 393, 914, 919, 672, 628, 
143, 103, 906, 717, 18, 324), V1 = c(52, 556, 222, 534, 953, 
346, 635, 84, 592, 444, 34, 340, 343, 188, 554, 397, 315, 643, 
376, 101, 663, 42, 360, 645, 718, 883, 266, 225, 674, 797, 726, 
259, 829, 701, 601, 206, 325, 963, 292, 985, 954, 828, 839, 541, 
301, 312, 187, 59, 563, 577, 961, 239, 147, 203, 421, 690, 542, 
412, 812, 19), V2 = c(354, 719, 45, 376, 921, 243, 256, 316, 
384, 450, 166, 850, 784, 291, 889, 389, 925, 157, 37, 528, 847, 
942, 624, 387, 680, 380, 848, 745, 49, 69, 864, 649, 125, 117, 
911, 947, 212, 628, 162, 165, 395, 437, 102, 136, 446, 51, 106, 
141, 886, 373, 113, 186, 233, 937, 698, 202, 89, 623, 731, 474
), V3 = c(18, 87, 692, 888, 681, 134, 774, 619, 544, 32, 804, 
993, 147, 352, 825, 490, 196, 794, 900, 796, 617, 160, 688, 947, 
665, 122, 386, 968, 772, 836, 696, 806, 925, 410, 949, 546, 303, 
550, 359, 285, 167, 605, 780, 419, 925, 822, 142, 4, 648, 18, 
867, 204, 617, 5, 251, 198, 316, 205, 660, 680), V4 = c(728, 
266, 678, 958, 946, 248, 425, 777, 86, 340, 527, 766, 161, 187, 
129, 881, 149, 888, 811, 118, 379, 22, 953, 940, 520, 200, 557, 
438, 401, 25, 55, 155, 73, 834, 614, 933, 235, 759, 852, 29, 
475, 356, 992, 765, 593, 703, 929, 823, 466, 717, 86, 607, 730, 
7, 416, 727, 400, 904, 503, 881), V5 = c(550, 785, 954, 852, 
718, 295, 208, 2, 36, 185, 726, 540, 476, 994, 720, 532, 401, 
525, 504, 868, 414, 878, 808, 550, 740, 9, 936, 570, 477, 516, 
561, 648, 686, 906, 387, 621, 461, 323, 829, 948, 964, 853, 943, 
805, 349, 254, 979, 784, 246, 444, 71, 883, 345, 973, 546, 120, 
310, 347, 732, 308)), class = "data.frame", row.names = c(NA, 
-60L))

Code I have tried....我试过的代码......

data <- gather(dataset, LAI, V1, V2, V3, V4, V5, -plot)

....provides these results (a sample of the resulting dataset): ....提供这些结果(结果数据集的样本):

plot genotype stage LAI  V1
1    101        1     1  V2 354
2    102        2     1  V2 719
3    103        3     1  V2  45
4    104        4     1  V2 376
5    105        5     1  V2 921
6    106        6     1  V2 243
7    107        7     1  V2 256
8    108        8     1  V2 316
9    109        9     1  V2 384
10   110       10     1  V2 450
11   111       11     1  V2 166
12   112       12     1  V2 850
13   113       13     1  V2 784
14   114       14     1  V2 291

The outcome needs to be like this:结果需要是这样的:

correct_format <- data.frame(genotype = c(1,
                                          2,
                                          3,
                                          4,
                                          5,
                                          6),
                             stage = c(1,
                                       1,
                                       1,
                                       1,
                                       1,
                                       1),
                             measurement = c("LAI",
                                             "LAI",
                                             "LAI",
                                             "LAI",
                                             "LAI",
                                             "LAI"),
                             value = c(822,
                                       763,
                                       551,
                                       251,
                                       800,
                                       761)

Perhaps, we need或许,我们需要

library(dplyr)
library(tidyr)
dataset %>%
    select(-plot) %>% 
    pivot_longer(cols = LAI:V5, names_to = 'measurement') %>% 
    arrange(measurement)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM