Search code examples
rtidyr

Is there an alternative to pivotlonger function in R?


I'm working on this microtiter data set, where I have measurements of a strain growing in a 96-well plate with different concentrations of DNase. I have taken the average of all the replicates and made the excel csv file, but also calculated the standard deviations in order to make error bars for my graph. I have a similar data set, but instead of time, I just have three different strains measured at different concentrations. I will insert the code below. Now, this code does not work for this data set, and I need to figure out a way to pivot the data set longer, but have the standard deviations tied to the mean values. I have tried the gather function also, but then it will duplicate the values, which is not what I want Please help with suggestions or tell me if my code is wrong

Data set imported from excel that has not been manipulated

Data set form excel that has not been manipulated

Data set with SD replicates, which is not wanted

Other data set that my code worked on

dput(B690DNaseI)
structure(list(Time = c("00:15", "00:30", "00:45", "01:00", "01:15", 
"01:30", "01:45", "02:00", "02:15", "02:30", "02:45", "03:00", 
"03:15", "03:30", "03:45", "04:00", "04:15", "04:30", "04:45", 
"05:00", "05:15", "05:30", "05:45", "06:00", "06:15", "06:30", 
"06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", 
"08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", 
"10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", 
"12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", 
"13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", 
"15:30", "15:45", "16:00"), LB = c(0.08775, 0.0875, 0.0875, 0.0875, 
0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 
0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.0875, 0.08775, 
0.088, 0.08775, 0.088, 0.08825, 0.08825, 0.08825, 0.0885, 0.0885, 
0.08875, 0.08875, 0.08925, 0.0895, 0.09, 0.09025, 0.09075, 0.0915, 
0.092, 0.0935, 0.0945, 0.0955, 0.09725, 0.09925, 0.101, 0.10175, 
0.10525, 0.10825, 0.1105, 0.113, 0.117, 0.121, 0.12375, 0.12625, 
0.1305, 0.13275, 0.1375, 0.139, 0.145, 0.15025, 0.15175, 0.1585, 
0.1655, 0.1705, 0.17425, 0.18125), C0 = c(0.16925, 0.20775, 0.23975, 
0.268, 0.29225, 0.316, 0.3385, 0.359, 0.38125, 0.401, 0.423, 
0.444, 0.4645, 0.48725, 0.51125, 0.53525, 0.563, 0.59825, 0.6375, 
0.682, 0.729, 0.7835, 0.84725, 0.8935, 0.9555, 0.9975, 1.0475, 
1.094, 1.141, 1.19, 1.2285, 1.27225, 1.30925, 1.34475, 1.3725, 
1.39875, 1.4255, 1.457, 1.47825, 1.495, 1.51825, 1.52675, 1.54475, 
1.57025, 1.57975, 1.5865, 1.60025, 1.60975, 1.6155, 1.6145, 1.625, 
1.63225, 1.63125, 1.6385, 1.63625, 1.6435, 1.63825, 1.63925, 
1.64625, 1.64225, 1.6405, 1.64475, 1.648, 1.6465), C10 = c(0.16925, 
0.20775, 0.23975, 0.268, 0.29225, 0.316, 0.3385, 0.359, 0.38125, 
0.401, 0.423, 0.444, 0.4645, 0.48725, 0.51125, 0.53525, 0.563, 
0.59825, 0.6375, 0.682, 0.729, 0.7835, 0.84725, 0.8935, 0.9555, 
0.9975, 1.0475, 1.094, 1.141, 1.19, 1.2285, 1.27225, 1.30925, 
1.34475, 1.3725, 1.39875, 1.4255, 1.457, 1.47825, 1.495, 1.51825, 
1.52675, 1.54475, 1.57025, 1.57975, 1.5865, 1.60025, 1.60975, 
1.6155, 1.6145, 1.625, 1.63225, 1.63125, 1.6385, 1.63625, 1.6435, 
1.63825, 1.63925, 1.64625, 1.64225, 1.6405, 1.64475, 1.648, 1.6465
), C20 = c(0.19975, 0.2285, 0.2555, 0.28525, 0.318, 0.3545, 0.3865, 
0.4155, 0.45, 0.482, 0.50825, 0.52975, 0.55625, 0.58325, 0.604, 
0.624, 0.6475, 0.67125, 0.69075, 0.71975, 0.75225, 0.77475, 0.82025, 
0.849, 0.88625, 0.9095, 0.94375, 0.979, 1.01425, 1.049, 1.0785, 
1.118, 1.146, 1.177, 1.21475, 1.237, 1.2685, 1.30125, 1.327, 
1.3485, 1.37475, 1.389, 1.4115, 1.43875, 1.44975, 1.458, 1.4745, 
1.48675, 1.49525, 1.50325, 1.5165, 1.52775, 1.5345, 1.54675, 
1.5545, 1.564, 1.5675, 1.5725, 1.58175, 1.583, 1.58575, 1.592, 
1.59725, 1.59825), C50 = c(0.1885, 0.23575, 0.27025, 0.30475, 
0.34175, 0.381, 0.41475, 0.44225, 0.478, 0.51325, 0.54375, 0.5625, 
0.5935, 0.625, 0.64875, 0.66425, 0.691, 0.7205, 0.7435, 0.778, 
0.8025, 0.83475, 0.87425, 0.90525, 0.95775, 0.973, 1.0115, 1.04125, 
1.07625, 1.111, 1.13875, 1.17525, 1.20875, 1.241, 1.2665, 1.29175, 
1.31675, 1.3515, 1.377, 1.397, 1.4235, 1.4375, 1.459, 1.48775, 
1.50175, 1.51375, 1.53175, 1.54675, 1.558, 1.5615, 1.5735, 1.58625, 
1.58975, 1.60325, 1.6065, 1.618, 1.6185, 1.6225, 1.632, 1.629, 
1.63025, 1.6355, 1.64, 1.641), C100 = c(0.17925, 0.20525, 0.25425, 
0.29275, 0.3235, 0.35625, 0.3905, 0.421, 0.457, 0.492, 0.51975, 
0.54425, 0.57275, 0.60175, 0.62725, 0.64775, 0.67375, 0.70325, 
0.722, 0.75525, 0.7915, 0.81525, 0.86425, 0.893, 0.93275, 0.959, 
0.998, 1.033, 1.06775, 1.10475, 1.13425, 1.172, 1.20575, 1.237, 
1.26375, 1.28875, 1.31125, 1.34075, 1.36375, 1.384, 1.40725, 
1.41925, 1.437, 1.4595, 1.4715, 1.4825, 1.49925, 1.5155, 1.52875, 
1.53975, 1.557, 1.574, 1.58275, 1.59725, 1.606, 1.6185, 1.622, 
1.6285, 1.6385, 1.6385, 1.642, 1.6485, 1.65225, 1.65475), SDLB = c(0.002487469, 
0.002061553, 0.002061553, 0.002061553, 0.002061553, 0.002061553, 
0.002061553, 0.002061553, 0.002061553, 0.002061553, 0.002061553, 
0.002061553, 0.002061553, 0.002061553, 0.002061553, 0.002061553, 
0.002061553, 0.002061553, 0.002061553, 0.002046338, 0.001870829, 
0.002046338, 0.001870829, 0.001920286, 0.001920286, 0.001920286, 
0.002061553, 0.002061553, 0.002277608, 0.002277608, 0.002384848, 
0.002692582, 0.002915476, 0.003031089, 0.003269174, 0.003570714, 
0.003937004, 0.004330127, 0.005024938, 0.0065, 0.007189402, 0.009120718, 
0.010839742, 0.012676257, 0.015221284, 0.018267115, 0.021639085, 
0.024217762, 0.027667671, 0.031717503, 0.034477348, 0.03692814, 
0.041051797, 0.043222535, 0.047882669, 0.049173163, 0.055113519, 
0.059838846, 0.061304058, 0.068053288, 0.074583845, 0.079197538, 
0.083175642, 0.089281507), SDC0 = c(0.020314711, 0.013754545, 
0.005165995, 0.005830952, 0.008699856, 0.015215124, 0.020742469, 
0.025426364, 0.029819247, 0.036311155, 0.041394444, 0.04573292, 
0.051742149, 0.058601941, 0.063841111, 0.067569871, 0.072852591, 
0.078477306, 0.078868562, 0.085173353, 0.093169201, 0.095473818, 
0.102008272, 0.092175105, 0.082235941, 0.070162312, 0.067459247, 
0.065738117, 0.065646782, 0.062685724, 0.057273467, 0.053881235, 
0.049393193, 0.045974857, 0.044466279, 0.040904615, 0.039016022, 
0.037033768, 0.035194993, 0.032855745, 0.030881831, 0.029097895, 
0.028437431, 0.028349383, 0.027307279, 0.026129485, 0.024752525, 
0.024262883, 0.024088379, 0.023584953, 0.025377155, 0.026545951, 
0.026752336, 0.02926175, 0.028717373, 0.03132491, 0.029345996, 
0.028411045, 0.029659526, 0.024539509, 0.0245, 0.02440671, 0.024768932, 
0.02368016), SDC10 = c(0.013143439, 0.011605494, 0.01498958, 
0.016515145, 0.024824383, 0.03482456, 0.040170729, 0.047283057, 
0.053110145, 0.058383966, 0.059434838, 0.061808171, 0.066640828, 
0.070881944, 0.073172314, 0.072682099, 0.075174381, 0.078445443, 
0.077483466, 0.081049368, 0.08525953, 0.084842501, 0.09012318, 
0.088075252, 0.086615169, 0.08116149, 0.079564439, 0.079937476, 
0.072706172, 0.070938354, 0.066417242, 0.064183331, 0.058836107, 
0.054974426, 0.049689033, 0.044528081, 0.045751366, 0.038693507, 
0.03278338, 0.030531746, 0.024304063, 0.020054613, 0.014515078, 
0.008642193, 0.012577261, 0.015247951, 0.016469669, 0.019817921, 
0.023155723, 0.025410382, 0.025124689, 0.026630575, 0.02725344, 
0.027860142, 0.027444262, 0.029054905, 0.029046299, 0.029371542, 
0.030392228, 0.027856777, 0.02723394, 0.026617663, 0.028864987, 
0.026947866), SDC20 = c(0.021844622, 0.011863811, 0.021569655, 
0.027878083, 0.036400549, 0.043985793, 0.049043348, 0.053368998, 
0.056493362, 0.057823006, 0.060155528, 0.062455484, 0.065731937, 
0.068765453, 0.071641468, 0.069745968, 0.069456821, 0.071628818, 
0.068896208, 0.070314917, 0.072223178, 0.06884902, 0.073506377, 
0.074545288, 0.077483466, 0.07047872, 0.070796804, 0.069061567, 
0.068100569, 0.067412907, 0.06425535, 0.060881032, 0.061899111, 
0.063035704, 0.050938075, 0.051200586, 0.046246621, 0.045844165, 
0.043611925, 0.040709336, 0.042522788, 0.041587258, 0.040332989, 
0.04293236, 0.043083495, 0.042148547, 0.042127782, 0.042938182, 
0.041336273, 0.03971382, 0.040782962, 0.03982697, 0.037646381, 
0.039168706, 0.035780581, 0.038542185, 0.035344731, 0.035089172, 
0.036485442, 0.033860006, 0.033484138, 0.034734709, 0.034909705, 
0.033221793), SDC50 = c(0.017670597, 0.011453711, 0.013663363, 
0.020522853, 0.029928039, 0.038059164, 0.043894049, 0.049119115, 
0.052139237, 0.055571463, 0.059289017, 0.06082968, 0.064863318, 
0.069079664, 0.07371355, 0.076185218, 0.081298831, 0.084328228, 
0.090946413, 0.100169856, 0.094309331, 0.104659866, 0.09936643, 
0.102738442, 0.116368757, 0.110609674, 0.106361412, 0.101233332, 
0.094078093, 0.092668765, 0.089923231, 0.090198046, 0.087650941, 
0.085565764, 0.081671598, 0.075204305, 0.073206472, 0.071765242, 
0.067944831, 0.06555532, 0.06326729, 0.057081083, 0.053740115, 
0.053765114, 0.052470825, 0.048509664, 0.046078059, 0.04099009, 
0.034532593, 0.030368569, 0.025352515, 0.023166517, 0.022708754, 
0.021264701, 0.020657928, 0.021342446, 0.022017039, 0.021937411, 
0.020211383, 0.018641352, 0.017795716, 0.017867568, 0.017248188, 
0.017535678), SDC100 = c(0.010062306, 0.011053845, 0.026570661, 
0.008093207, 0.006609652, 0.012968712, 0.020168044, 0.022498611, 
0.024418231, 0.022653642, 0.019765816, 0.015206906, 0.01662077, 
0.014463316, 0.015974589, 0.018179659, 0.021914607, 0.027390692, 
0.031464265, 0.038629652, 0.051823619, 0.051373023, 0.052394537, 
0.047877448, 0.052471421, 0.049786419, 0.047267193, 0.049332418, 
0.049001913, 0.044885967, 0.035981766, 0.028486839, 0.026310644, 
0.025913317, 0.026808348, 0.024924887, 0.020141686, 0.019600702, 
0.019447043, 0.018090053, 0.01662077, 0.018471261, 0.025666126, 
0.026873546, 0.02078912, 0.016976086, 0.017282578, 0.019422925, 
0.021229402, 0.023140873, 0.027206387, 0.032653484, 0.03911122, 
0.041117971, 0.040798284, 0.036656343, 0.030752032, 0.026776856, 
0.019904459, 0.010012492, 0.005448624, 0.015122417, 0.027901613, 
0.042408726)), class = "data.frame", row.names = c(NA, -64L))
Test <- B690DNaseI %>%
  pivot_longer(!Time,
               names_to = c("Concentration", ".value"),
               names_pattern = c("([[:alnum:]]+)_([[:alpha:]]+)")) %>%
  rename_with(~ paste("SD_", ., sep = ""), !c(Time, Concentration))

This code worked for previous data set, but not this one

The gather function did not work either

PB690 <- B690DNaseI %>% gather(SD_column, SD_value, starts_with("SD"))

Minimal input

small = structure(list(Time = c("00:15", "00:30", "00:45"), LB = c(0.08775, 
0.0875, 0.0875), C0 = c(0.16925, 0.20775, 0.23975), C10 = c(0.16925, 
0.20775, 0.23975), SDLB = c(0.002487469, 0.002061553, 0.002061553
), SDC0 = c(0.020314711, 0.013754545, 0.005165995), SDC10 = c(0.013143439, 
0.011605494, 0.01498958)), row.names = c(NA, 3L), class = "data.frame")
small
#    Time      LB      C0     C10        SDLB        SDC0      SDC10
# 1 00:15 0.08775 0.16925 0.16925 0.002487469 0.020314711 0.01314344
# 2 00:30 0.08750 0.20775 0.20775 0.002061553 0.013754545 0.01160549
# 3 00:45 0.08750 0.23975 0.23975 0.002061553 0.005165995 0.01498958

Solution

  • I think the issue is that you want a mean and SD column for each Concentration at each Time. You could do this by first renaming your columns systematically and then using pivot_longer

    library(tidyverse)
    
    B690DNaseI %>% 
      rename_with(~ifelse(substr(.x, 1, 2) == "SD", 
                          gsub("SD", "SD_", .x), paste0("mean_", .x)),
                  .cols = -1) %>% 
      pivot_longer(-1, names_sep = "_", names_to = c(".value", "Concentration"))
    #> # A tibble: 384 x 4
    #>    Time  Concentration   mean      SD
    #>    <chr> <chr>          <dbl>   <dbl>
    #>  1 00:15 LB            0.0878 0.00249
    #>  2 00:15 C0            0.169  0.0203 
    #>  3 00:15 C10           0.169  0.0131 
    #>  4 00:15 C20           0.200  0.0218 
    #>  5 00:15 C50           0.188  0.0177 
    #>  6 00:15 C100          0.179  0.0101 
    #>  7 00:30 LB            0.0875 0.00206
    #>  8 00:30 C0            0.208  0.0138 
    #>  9 00:30 C10           0.208  0.0116 
    #> 10 00:30 C20           0.228  0.0119 
    #> # i 374 more rows