I have a list of data frames -results1- where the data frames look like this (but with more rows)
names coefficients
1 ..a15.pdf 1.27679608
2 ..a17.pdf 1.05090176
I want to remove the dots before the variables in column 'names', i.e. change "..a15.pdf" to "a15.pdf".
I tried, with no success, different variations of
results1<-lapply(results1, function(x) {gsub("^.{0,2}", "", lapply(x, "[", "names"));x})
First two data frames from the list:
dput(results1[c(1,2)])
list(structure(list(names = c("..a15.pdf", "..a17.pdf", "..a18.pdf",
"..a21.pdf", "..a2TTT.pdf", "..a5.pdf", "..B11.pdf", "..B12.pdf",
"..B13.pdf", "..B22.pdf", "..B24.pdf", "..B4.pdf", "..B7.pdf",
"..B8.pdf", "..cw10-1.pdf", "..cw15-1TTT.pdf", "..cw17-1.pdf",
"..cw18.pdf", "..cw3.pdf", "..cw4.pdf", "..cw7_1TTT.pdf", "..cw13-1.pdf"
), coefficients = c(1.27679607834331, 1.05090175857491, 1.51820192474905,
2.30296037386815, 1.48568731934637, 0.493713103224402, 1.02705905465749,
0.999747360884078, 2.40828101927852, 0.695152132033603, 2.1436001615064,
2.25444037842867, 0.909773940025014, 1.14837173756827, -1.36323271003293,
0.341428535787024, -0.786878348480425, 0.793720472787986, -1.57831038567642,
0.277733503122777, -0.0364645818969112, -18.336668416705)), class = "data.frame", row.names = c(NA,
-22L)), structure(list(names = c("..a15.pdf", "..a17.pdf", "..a18.pdf",
"..a21.pdf", "..a2TTT.pdf", "..a5.pdf", "..B11.pdf", "..B12.pdf",
"..B13.pdf", "..B22.pdf", "..B24.pdf", "..B4.pdf", "..B7.pdf",
"..B8.pdf", "..cw10-1.pdf", "..cw15-1TTT.pdf", "..cw17-1.pdf",
"..cw18.pdf", "..cw3.pdf", "..cw4.pdf", "..cw7_1TTT.pdf", "..cw13-1.pdf"
), coefficients = c(2.096687569578, 2.19826038300833, 1.91814204277357,
0.801448541154512, 2.16169560949165, 1.48585130705963, 0.95126061691997,
1.93116618236938, 1.92555316191766, 1.00560861920225, 2.91129684208931,
2.75687804718002, 1.31164431967781, 2.22449059765255, -1.22629519335285,
1.31168579553008, -17.5786422399896, 1.25323523754693, -0.754445550651364,
0.555577381430987, 0.577850999404076, -34.2662973287062)), class = "data.frame", row.names = c(NA,
-22L)))
You have to escape . in gsub by using \\ two backslashes. This will replace all (any number of) dots preceeding the actual name.
results <- list(structure(list(names = c("..a15.pdf", "..a17.pdf", "..a18.pdf",
"..a21.pdf", "..a2TTT.pdf", "..a5.pdf", "..B11.pdf", "..B12.pdf",
"..B13.pdf", "..B22.pdf", "..B24.pdf", "..B4.pdf", "..B7.pdf",
"..B8.pdf", "..cw10-1.pdf", "..cw15-1TTT.pdf", "..cw17-1.pdf",
"..cw18.pdf", "..cw3.pdf", "..cw4.pdf", "..cw7_1TTT.pdf", "..cw13-1.pdf"
), coefficients = c(1.27679607834331, 1.05090175857491, 1.51820192474905,
2.30296037386815, 1.48568731934637, 0.493713103224402, 1.02705905465749,
0.999747360884078, 2.40828101927852, 0.695152132033603, 2.1436001615064,
2.25444037842867, 0.909773940025014, 1.14837173756827, -1.36323271003293,
0.341428535787024, -0.786878348480425, 0.793720472787986, -1.57831038567642,
0.277733503122777, -0.0364645818969112, -18.336668416705)), class = "data.frame", row.names = c(NA,
-22L)), structure(list(names = c("..a15.pdf", "..a17.pdf", "..a18.pdf",
"..a21.pdf", "..a2TTT.pdf", "..a5.pdf", "..B11.pdf", "..B12.pdf",
"..B13.pdf", "..B22.pdf", "..B24.pdf", "..B4.pdf", "..B7.pdf",
"..B8.pdf", "..cw10-1.pdf", "..cw15-1TTT.pdf", "..cw17-1.pdf",
"..cw18.pdf", "..cw3.pdf", "..cw4.pdf", "..cw7_1TTT.pdf", "..cw13-1.pdf"
), coefficients = c(2.096687569578, 2.19826038300833, 1.91814204277357,
0.801448541154512, 2.16169560949165, 1.48585130705963, 0.95126061691997,
1.93116618236938, 1.92555316191766, 1.00560861920225, 2.91129684208931,
2.75687804718002, 1.31164431967781, 2.22449059765255, -1.22629519335285,
1.31168579553008, -17.5786422399896, 1.25323523754693, -0.754445550651364,
0.555577381430987, 0.577850999404076, -34.2662973287062)), class = "data.frame", row.names = c(NA,
-22L)))
library(tidyverse)
results %>% map(~ .x %>% mutate(names = gsub('^\\.*(.*)$', '\\1', names)))
#> [[1]]
#> names coefficients
#> 1 a15.pdf 1.27679608
#> 2 a17.pdf 1.05090176
#> 3 a18.pdf 1.51820192
#> 4 a21.pdf 2.30296037
#> 5 a2TTT.pdf 1.48568732
#> 6 a5.pdf 0.49371310
#> 7 B11.pdf 1.02705905
#> 8 B12.pdf 0.99974736
#> 9 B13.pdf 2.40828102
#> 10 B22.pdf 0.69515213
#> 11 B24.pdf 2.14360016
#> 12 B4.pdf 2.25444038
#> 13 B7.pdf 0.90977394
#> 14 B8.pdf 1.14837174
#> 15 cw10-1.pdf -1.36323271
#> 16 cw15-1TTT.pdf 0.34142854
#> 17 cw17-1.pdf -0.78687835
#> 18 cw18.pdf 0.79372047
#> 19 cw3.pdf -1.57831039
#> 20 cw4.pdf 0.27773350
#> 21 cw7_1TTT.pdf -0.03646458
#> 22 cw13-1.pdf -18.33666842
#>
#> [[2]]
#> names coefficients
#> 1 a15.pdf 2.0966876
#> 2 a17.pdf 2.1982604
#> 3 a18.pdf 1.9181420
#> 4 a21.pdf 0.8014485
#> 5 a2TTT.pdf 2.1616956
#> 6 a5.pdf 1.4858513
#> 7 B11.pdf 0.9512606
#> 8 B12.pdf 1.9311662
#> 9 B13.pdf 1.9255532
#> 10 B22.pdf 1.0056086
#> 11 B24.pdf 2.9112968
#> 12 B4.pdf 2.7568780
#> 13 B7.pdf 1.3116443
#> 14 B8.pdf 2.2244906
#> 15 cw10-1.pdf -1.2262952
#> 16 cw15-1TTT.pdf 1.3116858
#> 17 cw17-1.pdf -17.5786422
#> 18 cw18.pdf 1.2532352
#> 19 cw3.pdf -0.7544456
#> 20 cw4.pdf 0.5555774
#> 21 cw7_1TTT.pdf 0.5778510
#> 22 cw13-1.pdf -34.2662973
Created on 2021-06-04 by the reprex package (v2.0.0)
Solution with tidyverse
library(purrr)
library(dplyr)
library(stringr)
map(results1, ~.x[]%>%
mutate(names = str_replace_all(names,"\\.\\.", "")))
[[1]]
names coefficients
1 a15.pdf 1.27679608
2 a17.pdf 1.05090176
3 a18.pdf 1.51820192
4 a21.pdf 2.30296037
5 a2TTT.pdf 1.48568732
6 a5.pdf 0.49371310
7 B11.pdf 1.02705905
8 B12.pdf 0.99974736
9 B13.pdf 2.40828102
10 B22.pdf 0.69515213
This worked for me:
lapply(your_list, function(df) dplyr::mutate(df, column = gsub(x = column, pattern = "pattern", replacement = "replacement")))
your_list- the list containing dataframes
column- the variable inside the dataframes where you want to do the gsub
In this way I found that the code did not change the class of the other variables in my dataframes
I have a data frame with 15 factor levels:
Value <- runif(225)
df <- data.frame(Variant=rep(c(1:15), each=15),
Value=Value)
df$Variant <- factor(df$Variant)
levels(df$Variant)
I am using ANOVA and after Duncan test:
model <- lm(Value~Variant, data=df)
anova(model)
library(agricolae)
out <- duncan.test(model, "Variant", group=F); out
After Duncan test I get the following comparison order of factor levels:
1-10,1-11,1-12,1-13,1-14,1-15,1-2,1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 10-11, 10-12, 10-13, 10-14, 10-15 etc.
How can I manually change this order like this:
1-2,1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 1-10, 1-11, 1-12, 1-13, 1-14, 1-15, 2-1, 2-2, 2-3, 2-4, 2-5, 2-6 etc.
Thank You very much.
You can do
ord <- do.call(rbind, lapply(strsplit(row.names(out$comparison), " - "), as.numeric))
out$comparison[order(ord[,1], ord[,2]),]
#> difference pvalue signif. LCL UCL
#> 1 - 2 0.038843696 0.7161 -0.16755260 0.24523999
#> 1 - 3 -0.082464843 0.4965 -0.31294877 0.14801909
#> 1 - 4 -0.026633590 0.8112 -0.23991700 0.18664982
#> 1 - 5 -0.008944756 0.9333 -0.21534105 0.19745153
#> 1 - 6 -0.113440226 0.3555 -0.34762702 0.12074656
#> 1 - 7 -0.033132833 0.7772 -0.25544634 0.18918067
#> 1 - 8 -0.006618202 0.9470 -0.20269177 0.18945536
#> 1 - 9 0.046838403 0.6741 -0.16644500 0.26012181
#> 1 - 10 -0.091429309 0.4543 -0.32388649 0.14102787
#> 1 - 11 -0.140899981 0.2522 -0.37661897 0.09481900
#> 1 - 12 0.001178870 0.9906 -0.19489469 0.19725243
#> 1 - 13 -0.047673502 0.6884 -0.27319857 0.17785157
#> 1 - 14 -0.031176107 0.7857 -0.24953106 0.18717885
#> 1 - 15 -0.074161352 0.5370 -0.30236480 0.15404210
#> 2 - 3 -0.121308539 0.3226 -0.35549533 0.11287825
#> 2 - 4 -0.065477286 0.5752 -0.28779079 0.15683622
#> 2 - 5 -0.047788453 0.6766 -0.26614341 0.17056650
#> 2 - 6 -0.152283922 0.2177 -0.38937226 0.08480442
#> 2 - 7 -0.071976529 0.5492 -0.30017998 0.15622692
#> 2 - 8 -0.045461898 0.6831 -0.25874530 0.16782151
#> 2 - 9 0.007994707 0.9360 -0.18807886 0.20406827
#> 3 - 4 0.055831254 0.6330 -0.16648225 0.27814476
#> 3 - 5 0.073520087 0.5354 -0.15200498 0.29904515
#> 3 - 6 -0.030975383 0.7719 -0.23737167 0.17542091
#> 3 - 7 0.049332010 0.6577 -0.16395140 0.26261542
#> 3 - 8 0.075846641 0.5277 -0.15235681 0.30405009
#> 3 - 9 0.129303246 0.2943 -0.10641574 0.36502223
#> 4 - 5 0.017688833 0.8590 -0.17838473 0.21376240
#> 4 - 6 -0.086806636 0.4694 -0.31501009 0.14139682
#> 4 - 7 -0.006499243 0.9515 -0.21289553 0.19989705
#> 4 - 8 0.020015388 0.8514 -0.18638090 0.22641168
#> 4 - 9 0.073471993 0.5357 -0.15205307 0.29899706
#> 5 - 6 -0.104495470 0.3877 -0.33497940 0.12598846
#> 5 - 7 -0.024188077 0.8282 -0.23747148 0.18909533
#> 5 - 8 0.002326554 0.9814 -0.19374701 0.19840012
#> 5 - 9 0.055783160 0.6333 -0.16653035 0.27809667
#> 6 - 7 0.080307393 0.4913 -0.14200611 0.30262090
#> 6 - 8 0.106822024 0.3812 -0.12563515 0.33927920
#> 6 - 9 0.160278629 0.1961 -0.07804271 0.39859996
#> 7 - 8 0.026514631 0.8172 -0.19184032 0.24486959
#> 7 - 9 0.079971236 0.5097 -0.15051269 0.31045517
#> 8 - 9 0.053456605 0.6407 -0.16489835 0.27181156
#> 10 - 2 0.130273005 0.2906 -0.10544598 0.36599199
#> 10 - 3 0.008964466 0.9283 -0.18710910 0.20503803
#> 10 - 4 0.064795720 0.5852 -0.16072935 0.29032079
#> 10 - 5 0.082484553 0.4920 -0.14571890 0.31068801
#> 10 - 6 -0.022010917 0.8251 -0.21808448 0.17406265
#> 10 - 7 0.058296476 0.6107 -0.16005848 0.27665143
#> 10 - 8 0.084811107 0.4842 -0.14567282 0.31529504
#> 10 - 9 0.138267713 0.2641 -0.09882063 0.37535605
#> 10 - 11 -0.049470671 0.6431 -0.25586696 0.15692562
#> 10 - 12 0.092608180 0.4517 -0.14157861 0.32679497
#> 10 - 13 0.043755807 0.6944 -0.16952760 0.25703921
#> 10 - 14 0.060253203 0.6062 -0.16206030 0.28256671
#> 10 - 15 0.017267958 0.8716 -0.18912833 0.22366425
#> 11 - 2 0.179743677 0.1456 -0.05857766 0.41806501
#> 11 - 3 0.058435138 0.5995 -0.15484827 0.27171854
#> 11 - 4 0.114266391 0.3442 -0.11621754 0.34475032
#> 11 - 5 0.131955224 0.2776 -0.10050195 0.36441240
#> 11 - 6 0.027459755 0.7828 -0.16861381 0.22353332
#> 11 - 7 0.107767148 0.3617 -0.11775792 0.33329222
#> 11 - 8 0.134281779 0.2726 -0.09990501 0.36846857
#> 11 - 9 0.187738384 0.1296 -0.05170032 0.42717709
#> 11 - 12 0.142078851 0.2509 -0.09500949 0.37916719
#> 11 - 13 0.093226479 0.4236 -0.12908703 0.31553999
#> 11 - 14 0.109723874 0.3591 -0.11847958 0.33792733
#> 11 - 15 0.066738629 0.5597 -0.15161633 0.28509358
#> 12 - 2 0.037664826 0.7053 -0.15840874 0.23373839
#> 12 - 3 -0.083643714 0.4940 -0.31610089 0.14881346
#> 12 - 4 -0.027812460 0.8084 -0.24616741 0.19054249
#> 12 - 5 -0.010123627 0.9277 -0.22340703 0.20315978
#> 12 - 6 -0.114619097 0.3535 -0.35033808 0.12109989
#> 12 - 7 -0.034311704 0.7730 -0.25983677 0.19121336
#> 12 - 8 -0.007797073 0.9418 -0.21419336 0.19859922
#> 12 - 9 0.045659533 0.6690 -0.16073676 0.25205582
#> 12 - 13 -0.048852373 0.6848 -0.27705583 0.17935108
#> 12 - 14 -0.032354977 0.7823 -0.25466848 0.18995853
#> 12 - 15 -0.075340222 0.5347 -0.30582415 0.15514371
#> 13 - 2 0.086517198 0.4754 -0.14396673 0.31700113
#> 13 - 3 -0.034791341 0.7447 -0.24118763 0.17160495
#> 13 - 4 0.021039912 0.8503 -0.19224349 0.23432332
#> 13 - 5 0.038728746 0.7355 -0.17962621 0.25708370
#> 13 - 6 -0.065766724 0.5655 -0.28412168 0.15258823
#> 13 - 7 0.014540669 0.8839 -0.18153289 0.21061423
#> 13 - 8 0.041055300 0.7258 -0.18125821 0.26336881
#> 13 - 9 0.094511905 0.4391 -0.13794527 0.32696908
#> 13 - 14 0.016497396 0.8773 -0.18989890 0.22289369
#> 13 - 15 -0.026487850 0.7903 -0.22256141 0.16958571
#> 14 - 2 0.070019803 0.5552 -0.15550526 0.29554487
#> 14 - 3 -0.051288737 0.6544 -0.26964369 0.16706622
#> 14 - 4 0.004542517 0.9636 -0.19153105 0.20061608
#> 14 - 5 0.022231350 0.8352 -0.18416494 0.22862764
#> 14 - 6 -0.082264120 0.4877 -0.30778919 0.14326095
#> 14 - 7 -0.001956726 0.9843 -0.19803029 0.19411684
#> 14 - 8 0.024557904 0.8256 -0.18872550 0.23784131
#> 14 - 9 0.078014510 0.5159 -0.15018894 0.30621796
#> 14 - 15 -0.042985245 0.6996 -0.25626865 0.17029816
#> 15 - 2 0.113005048 0.3539 -0.11945213 0.34546222
#> 15 - 3 -0.008303492 0.9335 -0.20437706 0.18777007
#> 15 - 4 0.047527762 0.6783 -0.17082719 0.26588272
#> 15 - 5 0.065216595 0.5768 -0.15709691 0.28753010
#> 15 - 6 -0.039278874 0.7244 -0.25256228 0.17400453
#> 15 - 7 0.041028519 0.7009 -0.16536777 0.24742481
#> 15 - 8 0.067543150 0.5693 -0.15798192 0.29306822
#> 15 - 9 0.120999755 0.3239 -0.11318703 0.35518654
EDIT
The OP has asked how this might be achieved with Roman numerals. In that case, you would need to do:
install.packages("gtools")
ord <- do.call(rbind,
lapply(strsplit(row.names(out$comparison), "\\W"),
function(x) gtools::roman2int(x[x != ""])))
out$comparison[order(ord[,1], ord[,2]),]