extract list elements and concatenate into a string in r - r
using the variable list below I want to for all combinations, join the variables into a string seperated by "+"
l_ALLVar_list <- c("a","b","c","d","z1","z2","z3")
I have the code to generate the 127 combinations
all_combos=do.call("c", lapply(seq_along(l_ALLVar_list), function(i) combn(l_ALLVar_list, i, FUN = list)))
and using position 66 as an example
> all_combos[66]
[[1]]
[1] "a" "b" "c" "z2"
I want to be able to join the elements of these at index 66 into the string a+b+c+z2
I have tried
str_c(c(lol[66]),collapse=',')
but it comes back as
c(\"weight\", \"length\", \"wheel_base\", \"city_mpg\")
paste(all_combos[66], collapse = '')
produces the same again
any help would be appreciated
You can use the FUN argument in combn to paste all the combinations of l_ALLVar_list in one call, eliminating the need for your all_combos list.
unlist(lapply(seq_along(l_ALLVar_list), combn, x=l_ALLVar_list, paste, collapse="+"))
# [1] "a" "b" "c" "d" "z1"
# [6] "z2" "z3" "a+b" "a+c" "a+d"
# [11] "a+z1" "a+z2" "a+z3" "b+c" "b+d"
# [16] "b+z1" "b+z2" "b+z3" "c+d" "c+z1"
# [21] "c+z2" "c+z3" "d+z1" "d+z2" "d+z3"
# [26] "z1+z2" "z1+z3" "z2+z3" "a+b+c" "a+b+d"
# [31] "a+b+z1" "a+b+z2" "a+b+z3" "a+c+d" "a+c+z1"
# [36] "a+c+z2" "a+c+z3" "a+d+z1" "a+d+z2" "a+d+z3"
# [41] "a+z1+z2" "a+z1+z3" "a+z2+z3" "b+c+d" "b+c+z1"
# [46] "b+c+z2" "b+c+z3" "b+d+z1" "b+d+z2" "b+d+z3"
# [51] "b+z1+z2" "b+z1+z3" "b+z2+z3" "c+d+z1" "c+d+z2"
# [56] "c+d+z3" "c+z1+z2" "c+z1+z3" "c+z2+z3" "d+z1+z2"
# [61] "d+z1+z3" "d+z2+z3" "z1+z2+z3" "a+b+c+d" "a+b+c+z1"
# [66] "a+b+c+z2" "a+b+c+z3" "a+b+d+z1" "a+b+d+z2" "a+b+d+z3"
# [71] "a+b+z1+z2" "a+b+z1+z3" "a+b+z2+z3" "a+c+d+z1" "a+c+d+z2"
# [76] "a+c+d+z3" "a+c+z1+z2" "a+c+z1+z3" "a+c+z2+z3" "a+d+z1+z2"
# [81] "a+d+z1+z3" "a+d+z2+z3" "a+z1+z2+z3" "b+c+d+z1" "b+c+d+z2"
# [86] "b+c+d+z3" "b+c+z1+z2" "b+c+z1+z3" "b+c+z2+z3" "b+d+z1+z2"
# [91] "b+d+z1+z3" "b+d+z2+z3" "b+z1+z2+z3" "c+d+z1+z2" "c+d+z1+z3"
# [96] "c+d+z2+z3" "c+z1+z2+z3" "d+z1+z2+z3" "a+b+c+d+z1" "a+b+c+d+z2"
#[101] "a+b+c+d+z3" "a+b+c+z1+z2" "a+b+c+z1+z3" "a+b+c+z2+z3" "a+b+d+z1+z2"
#[106] "a+b+d+z1+z3" "a+b+d+z2+z3" "a+b+z1+z2+z3" "a+c+d+z1+z2" "a+c+d+z1+z3"
#[111] "a+c+d+z2+z3" "a+c+z1+z2+z3" "a+d+z1+z2+z3" "b+c+d+z1+z2" "b+c+d+z1+z3"
#[116] "b+c+d+z2+z3" "b+c+z1+z2+z3" "b+d+z1+z2+z3" "c+d+z1+z2+z3" "a+b+c+d+z1+z2"
#[121] "a+b+c+d+z1+z3" "a+b+c+d+z2+z3" "a+b+c+z1+z2+z3" "a+b+d+z1+z2+z3" "a+c+d+z1+z2+z3"
#[126] "b+c+d+z1+z2+z3" "a+b+c+d+z1+z2+z3"
Use lapply to do paste for each item in your list:
result <- unlist(lapply(all_combos,
function(c) do.call(paste, c(as.list(c), sep="+"))))
> result[66:70]
[1] "a+b+c+z2" "a+b+c+z3" "a+b+d+z1" "a+b+d+z2" "a+b+d+z3"
Related
Using R, How do I copy the tibble to an element of the list. for example, each element like ff[i] have a nibble at each i
Use vector() to create an empty vector called ff that is of mode “list” and length 9. Now write a for() loop to loop over the 9 files in dfiles and for each (i) read the file in to a tibble, and change the column names to x and y, and (ii) copy the tibble to an element of your list ff. dfiles is a directory which has different files. This is what I did. ff <- vector(mode = "list", length = 9) length <- length(dfiles) for (i in 1:length) { study <- read_csv(dfiles[i]) names(study)[1] <- "x" names(study)[2] <- "y" ff[i] <- c(study) print(head(ff[i])) } [[1]] [1] -0.989532202 -0.052799402 0.823610903 -0.255509103 -0.220684347 [6] 0.307726791 -0.060013253 -0.555652890 -0.138615019 1.882839792 [11] 0.873668680 -0.914597073 -1.244917622 -0.359982241 1.328774701 [16] 0.292679118 -0.701505237 0.882234568 -0.133370389 -1.120678499 [21] 0.461192454 1.524142810 0.434468298 0.192000371 -0.656243128 [26] 0.568398531 -1.070570535 -1.653149024 -0.043352768 -0.034593506 [31] 2.365055532 -1.216347308 0.170906323 0.805053094 1.050592844 [36] -0.010724485 -0.743256141 -0.065784052 1.939755992 0.482739008 [41] -2.044477073 1.423459129 0.540502661 -0.033571772 -0.017863621 [46] -0.149789720 0.256559481 -0.503866933 0.277011252 -0.931356025 [51] 0.200146875 1.106837421 0.509206114 1.033749676 -1.090868762 [56] 0.054792784 0.617250303 -1.068004868 1.565814337 -1.034808011 [61] 0.164518709 0.151832330 0.121670302 -0.210424584 0.449936787 [66] -1.031164492 -1.289364188 -0.654568638 -0.057324104 1.256747820 [71] 1.587454140 0.319481463 0.381591623 -0.243644884 0.048053084 [76] -1.404545861 0.289933729 -0.535553582 0.334678773 -0.345981339 [81] -0.661615735 -0.219111377 -0.366904911 1.094578208 0.209208082 [86] 0.432491426 -1.240853586 1.496821710 0.159370441 -0.856281403 [91] 0.309046645 0.870434030 -1.383677138 1.690106970 -0.158030705 [96] 1.121170781 0.072261319 -0.332422845 -1.834920047 -1.100172219 [101] -0.041340300 0.827852545 -1.881678654 1.375441112 1.398990464 [106] -1.143316256 0.472300562 -1.033639213 -0.125199979 0.928662739 [111] 0.868339648 -0.849174604 -0.386636454 -0.976163571 0.339543660 [116] -1.559075164 -2.629325442 1.469812282 2.273472913 -0.455033540 [121] 0.761102487 -0.007502784 1.474313800 and the following error. 1: In ff[i] <- c(study) : number of items to replace is not a multiple of replacement length 2: In ff[i] <- c(study) : I was expecting that it'll still have column names so I am not sure how to fix it and where I am going wrong.
Was supposed to use double brackets. ff[[i]] <- study would fix the problem.
How to generate all string permutations with given sets of letters at each position in R
I am sure this has been asked and solved before, but probably I am searching for the wrong terms. I cannot find the relevant thread. In R, I would like to generate all possible words / strings, where each position can take only a set of values, like pos1 can be ABC pos2 can be ABCD pos3 can be ABC pos4 can be BCD etc. Eg.: BABC is a solution but DABC is not. If you can point me towards a solution, I would really appreciate! Thanks for your time! ... timb!, timc!, timd! ... thx, Bud
In Base R we can do the following pos1 <- c('A','B','C') pos2 <- c('A','B','C','D') pos4 <- c('B','C','D') AllPos <- list(pos1,pos2,pos3,pos4) result <- AllPos[1] for(i in AllPos[-1] ){ result <- apply(merge(result ,i),1,paste0,collapse="") } > result [1] "AAAB" "BAAB" "CAAB" "ABAB" "BBAB" "CBAB" "ACAB" "BCAB" "CCAB" "ADAB" [11] "BDAB" "CDAB" "AABB" "BABB" "CABB" "ABBB" "BBBB" "CBBB" "ACBB" "BCBB" [21] "CCBB" "ADBB" "BDBB" "CDBB" "AACB" "BACB" "CACB" "ABCB" "BBCB" "CBCB" [31] "ACCB" "BCCB" "CCCB" "ADCB" "BDCB" "CDCB" "AAAC" "BAAC" "CAAC" "ABAC" [41] "BBAC" "CBAC" "ACAC" "BCAC" "CCAC" "ADAC" "BDAC" "CDAC" "AABC" "BABC" [51] "CABC" "ABBC" "BBBC" "CBBC" "ACBC" "BCBC" "CCBC" "ADBC" "BDBC" "CDBC" [61] "AACC" "BACC" "CACC" "ABCC" "BBCC" "CBCC" "ACCC" "BCCC" "CCCC" "ADCC" [71] "BDCC" "CDCC" "AAAD" "BAAD" "CAAD" "ABAD" "BBAD" "CBAD" "ACAD" "BCAD" [81] "CCAD" "ADAD" "BDAD" "CDAD" "AABD" "BABD" "CABD" "ABBD" "BBBD" "CBBD" [91] "ACBD" "BCBD" "CCBD" "ADBD" "BDBD" "CDBD" "AACD" "BACD" "CACD" "ABCD" [101] "BBCD" "CBCD" "ACCD" "BCCD" "CCCD" "ADCD" "BDCD" "CDCD"
A quick and dirty base R solution... p1 <- "ABC" p2 <- "ABCD" p3 <- "ABC" p4 <- "BCD" apply(expand.grid(strsplit(p1, "")[[1]], strsplit(p2, "")[[1]], strsplit(p3, "")[[1]], strsplit(p4, "")[[1]]), 1, paste0, collapse = "") #> [1] "AAAB" "BAAB" "CAAB" "ABAB" "BBAB" "CBAB" "ACAB" "BCAB" "CCAB" "ADAB" #> [11] "BDAB" "CDAB" "AABB" "BABB" "CABB" "ABBB" "BBBB" "CBBB" "ACBB" "BCBB" #> [21] "CCBB" "ADBB" "BDBB" "CDBB" "AACB" "BACB" "CACB" "ABCB" "BBCB" "CBCB" #> [31] "ACCB" "BCCB" "CCCB" "ADCB" "BDCB" "CDCB" "AAAC" "BAAC" "CAAC" "ABAC" #> [41] "BBAC" "CBAC" "ACAC" "BCAC" "CCAC" "ADAC" "BDAC" "CDAC" "AABC" "BABC" #> [51] "CABC" "ABBC" "BBBC" "CBBC" "ACBC" "BCBC" "CCBC" "ADBC" "BDBC" "CDBC" #> [61] "AACC" "BACC" "CACC" "ABCC" "BBCC" "CBCC" "ACCC" "BCCC" "CCCC" "ADCC" #> [71] "BDCC" "CDCC" "AAAD" "BAAD" "CAAD" "ABAD" "BBAD" "CBAD" "ACAD" "BCAD" #> [81] "CCAD" "ADAD" "BDAD" "CDAD" "AABD" "BABD" "CABD" "ABBD" "BBBD" "CBBD" #> [91] "ACBD" "BCBD" "CCBD" "ADBD" "BDBD" "CDBD" "AACD" "BACD" "CACD" "ABCD" #> [101] "BBCD" "CBCD" "ACCD" "BCCD" "CCCD" "ADCD" "BDCD" "CDCD" Created on 2020-06-18 by the reprex package (v0.3.0)
expand.grid is your friend here. A simple solution: apply(expand.grid(list( LETTERS[1:3], LETTERS[1:4], LETTERS[1:3], LETTERS[2:4])), 1, paste, collapse = "") #> [1] "AAAB" "BAAB" "CAAB" "ABAB" "BBAB" "CBAB" "ACAB" "BCAB" "CCAB" "ADAB" #> [11] "BDAB" "CDAB" "AABB" "BABB" "CABB" "ABBB" "BBBB" "CBBB" "ACBB" "BCBB" #> [21] "CCBB" "ADBB" "BDBB" "CDBB" "AACB" "BACB" "CACB" "ABCB" "BBCB" "CBCB" #> [31] "ACCB" "BCCB" "CCCB" "ADCB" "BDCB" "CDCB" "AAAC" "BAAC" "CAAC" "ABAC" #> [41] "BBAC" "CBAC" "ACAC" "BCAC" "CCAC" "ADAC" "BDAC" "CDAC" "AABC" "BABC" #> [51] "CABC" "ABBC" "BBBC" "CBBC" "ACBC" "BCBC" "CCBC" "ADBC" "BDBC" "CDBC" #> [61] "AACC" "BACC" "CACC" "ABCC" "BBCC" "CBCC" "ACCC" "BCCC" "CCCC" "ADCC" #> [71] "BDCC" "CDCC" "AAAD" "BAAD" "CAAD" "ABAD" "BBAD" "CBAD" "ACAD" "BCAD" #> [81] "CCAD" "ADAD" "BDAD" "CDAD" "AABD" "BABD" "CABD" "ABBD" "BBBD" "CBBD" #> [91] "ACBD" "BCBD" "CCBD" "ADBD" "BDBD" "CDBD" "AACD" "BACD" "CACD" "ABCD" #> [101] "BBCD" "CBCD" "ACCD" "BCCD" "CCCD" "ADCD" "BDCD" "CDCD" Created on 2020-06-18 by the reprex package (v0.3.0)
How to output in R all possible deviations of a word for a fixed distance value?
I have a word and want to output in R all possible deviatons (replacement, substitution, insertion) for a fixed distance value into a vector. For instance, the word "Cat" and a fixed distance value of 1 results in a vector with the elements "cot", "at", ...
I'm going to assume that you want all actual words, not just permutations of the characters with an edit distance of 1 that would include non-words such as "zat". We can do this using adist() to compute the edit distance between your target word and all eligible English words, taken from some word list. Here, I used the English syllable dictionary from the quanteda package (you did tag this question as quanteda after all) but this could have been any vector of English dictionary words from any other source as well. To narrow things down, we first exclude all words that are different in length from the target word by your distance value. distfn <- function(word, distance = 1) { # select eligible words for efficiency eligible_y_words <- names(quanteda::data_int_syllables) wordlengths <- nchar(eligible_y_words) eligible_y_words <- eligible_y_words[wordlengths >= (nchar(word) - distance) & wordlengths <= (nchar(word) + distance)] # compute Levenshtein distance distances <- utils::adist(word, eligible_y_words)[1, ] # return only those for the requested distance value eligible_y_words[distances == distance] } distfn("cat", 1) ## [1] "at" "bat" "ca" "cab" "cac" "cad" "cai" "cal" "cam" "can" ## [11] "cant" "cao" "cap" "caq" "car" "cart" "cas" "cast" "cate" "cato" ## [21] "cats" "catt" "cau" "caw" "cay" "chat" "coat" "cot" "ct" "cut" ## [31] "dat" "eat" "fat" "gat" "hat" "kat" "lat" "mat" "nat" "oat" ## [41] "pat" "rat" "sat" "scat" "tat" "vat" "wat" To demonstrate how this works on longer words, with alternative distance values. distfn("coffee", 1) ## [1] "caffee" "coffeen" "coffees" "coffel" "coffer" "coffey" "cuffee" ## [8] "toffee" distfn("coffee", 2) ## [1] "caffey" "calfee" "chafee" "chaffee" "cofer" "coffee's" ## [7] "coffelt" "coffers" "coffin" "cofide" "cohee" "coiffe" ## [13] "coiffed" "colee" "colfer" "combee" "comfed" "confer" ## [19] "conlee" "coppee" "cottee" "coulee" "coutee" "cuffe" ## [25] "cuffed" "diffee" "duffee" "hoffer" "jaffee" "joffe" ## [31] "mcaffee" "moffet" "noffke" "offen" "offer" "roffe" ## [37] "scoffed" "soffel" "soffer" "yoffie" (Yes, according to the CMU pronunciation dictionary, those are all actual words...) EDIT: Make for all permutations of letters, not just actual words This involves permutations from the alphabet that have the fixed edit distances from the input word. Here I've done it not particular efficiently by forming all permutations of letters within the eligible ranges, and then computing their edit distance from the target word, and then selecting them. So it's a variation of above, except instead of a dictionary, it uses permuted words. distfn2 <- function(word, distance = 1) { result <- character() # start with deletions for (i in max((nchar(word) - distance), 0):(nchar(word) - 1)) { result <- c( result, combn(unlist(strsplit(word, "", fixed = TRUE)), i, paste, collapse = "", simplify = TRUE ) ) } # now for changes and insertions for (i in (nchar(word)):(nchar(word) + distance)) { # all possible edits edits <- apply(expand.grid(rep(list(letters), i)), 1, paste0, collapse = "" ) # remove original word edits <- edits[edits != word] # get all distances, add to result distances <- utils::adist(word, edits)[1, ] result <- c(result, edits[distances == distance]) } result } For the OP example: distfn2("cat", 1) ## [1] "ca" "ct" "at" "caa" "cab" "cac" "cad" "cae" "caf" "cag" ## [11] "cah" "cai" "caj" "cak" "cal" "cam" "can" "cao" "cap" "caq" ## [21] "car" "cas" "aat" "bat" "dat" "eat" "fat" "gat" "hat" "iat" ## [31] "jat" "kat" "lat" "mat" "nat" "oat" "pat" "qat" "rat" "sat" ## [41] "tat" "uat" "vat" "wat" "xat" "yat" "zat" "cbt" "cct" "cdt" ## [51] "cet" "cft" "cgt" "cht" "cit" "cjt" "ckt" "clt" "cmt" "cnt" ## [61] "cot" "cpt" "cqt" "crt" "cst" "ctt" "cut" "cvt" "cwt" "cxt" ## [71] "cyt" "czt" "cau" "cav" "caw" "cax" "cay" "caz" "cata" "catb" ## [81] "catc" "catd" "cate" "catf" "catg" "cath" "cati" "catj" "catk" "catl" ## [91] "catm" "catn" "cato" "catp" "catq" "catr" "cats" "caat" "cbat" "acat" ## [101] "bcat" "ccat" "dcat" "ecat" "fcat" "gcat" "hcat" "icat" "jcat" "kcat" ## [111] "lcat" "mcat" "ncat" "ocat" "pcat" "qcat" "rcat" "scat" "tcat" "ucat" ## [121] "vcat" "wcat" "xcat" "ycat" "zcat" "cdat" "ceat" "cfat" "cgat" "chat" ## [131] "ciat" "cjat" "ckat" "clat" "cmat" "cnat" "coat" "cpat" "cqat" "crat" ## [141] "csat" "ctat" "cuat" "cvat" "cwat" "cxat" "cyat" "czat" "cabt" "cact" ## [151] "cadt" "caet" "caft" "cagt" "caht" "cait" "cajt" "cakt" "calt" "camt" ## [161] "cant" "caot" "capt" "caqt" "cart" "cast" "catt" "caut" "cavt" "cawt" ## [171] "caxt" "cayt" "cazt" "catu" "catv" "catw" "catx" "caty" "catz" Also works with other edit distances, although it becomes very slow for longer words. d2 <- distfn2("cat", 2) set.seed(100) c(head(d2, 50), sample(d2, 50), tail(d2, 50)) ## [1] "c" "a" "t" "ca" "ct" "at" "aaa" "baa" ## [9] "daa" "eaa" "faa" "gaa" "haa" "iaa" "jaa" "kaa" ## [17] "laa" "maa" "naa" "oaa" "paa" "qaa" "raa" "saa" ## [25] "taa" "uaa" "vaa" "waa" "xaa" "yaa" "zaa" "cba" ## [33] "aca" "bca" "cca" "dca" "eca" "fca" "gca" "hca" ## [41] "ica" "jca" "kca" "lca" "mca" "nca" "oca" "pca" ## [49] "qca" "rca" "cnts" "cian" "pcatb" "cqo" "uawt" "hazt" ## [57] "cpxat" "aaet" "ckata" "caod" "ncatl" "qcamt" "cdtp" "qajt" ## [65] "bckat" "qcatr" "cqah" "rcbt" "cvbt" "bbcat" "vcaz" "ylcat" ## [73] "cahz" "jcgat" "mant" "jatd" "czlat" "cbamt" "cajta" "cafp" ## [81] "cizt" "cmaut" "qwat" "jcazt" "hdcat" "ucant" "hate" "cajtl" ## [89] "caaty" "cix" "nmat" "cajit" "cmnat" "caobt" "catoi" "ncau" ## [97] "ucoat" "ncamt" "jath" "oats" "chatz" "ciatz" "cjatz" "ckatz" ## [105] "clatz" "cmatz" "cnatz" "coatz" "cpatz" "cqatz" "cratz" "csatz" ## [113] "ctatz" "cuatz" "cvatz" "cwatz" "cxatz" "cyatz" "czatz" "cabtz" ## [121] "cactz" "cadtz" "caetz" "caftz" "cagtz" "cahtz" "caitz" "cajtz" ## [129] "caktz" "caltz" "camtz" "cantz" "caotz" "captz" "caqtz" "cartz" ## [137] "castz" "cattz" "cautz" "cavtz" "cawtz" "caxtz" "caytz" "caztz" ## [145] "catuz" "catvz" "catwz" "catxz" "catyz" "catzz" This could be speeded up by less brute force formation of all permutations and then applying adist() to them - it could consist of changes or insertions of known edit distances generated algorithmically from letters.
Vectorize over all combinations of arguments
Is there a way to vectorize an R function over all combinations of multiple parameters and return the result as a list? As an example, using Vectorize over rnorm produces the following, but I would like to have a list of vectors corresponding to each combination of the arguments (so it should return a list of 60 vectors instead of just 5): > vrnorm <- Vectorize(rnorm) > vrnorm( 10*1:5, mean = 1:4, sd = 1:3) [[1]] [1] 1.37858918 -0.85432372 1.87321175 2.08362291 0.02950438 1.67967249 [7] 2.25954748 1.44031251 0.09816078 0.91365201 [[2]] [1] 1.7717267 1.7961157 2.3291686 2.6114272 2.6228930 -0.2580403 [7] 3.3232109 -0.4652434 -0.4803258 -0.1170871 0.1158350 -1.0902252 [13] -0.6400934 3.6625290 2.5924096 4.5878564 0.7265718 3.2034281 [19] -0.2499768 2.0164275 [[3]] [1] 5.8251252 3.1089121 2.8893594 2.9079357 1.9308677 4.3359878 [7] -0.3668157 4.9728508 -0.6494110 6.7729562 6.1623976 -0.1696638 [13] 5.4664038 3.8141798 -3.1842879 2.3985010 0.3840465 4.0696628 [19] 4.8217798 3.3135100 4.9028273 3.6193840 4.8861864 3.9871897 [25] -0.1059491 3.8961742 4.8293925 3.8935335 6.3194862 4.7846143 [[4]] [1] 3.737043 2.849215 4.611868 3.494396 2.909659 4.861474 2.000194 3.343171 [9] 4.019523 3.277575 3.885272 3.331160 4.581551 4.960162 3.061960 5.359514 [17] 4.651848 3.640535 3.612368 4.338019 5.233665 3.585976 4.018191 4.320883 [25] 2.598541 3.519587 5.231375 4.733647 2.493334 2.791483 4.330052 2.498424 [33] 3.317115 3.515012 5.079780 4.720884 3.055191 5.262385 1.939961 4.779480 [[5]] [1] 4.31697756 0.93754587 3.96698522 -0.03680018 1.94987430 1.73985617 [7] -1.42300550 2.07764933 0.45701395 2.42548257 0.67745524 -2.42054060 [13] 1.14655845 1.60277193 -1.04636658 0.94097335 3.07688803 0.58049012 [19] 1.25812532 1.91613097 -2.95408979 3.00990345 -0.67314868 0.64746260 [25] 1.69640497 0.68493689 2.84261574 1.65290227 4.16990548 -3.30426803 [31] 3.80508273 5.95888355 -0.09021591 3.88157980 -1.19166351 2.70208228 [37] -0.56278834 -0.83943824 -0.86868493 -1.19995506 -2.30275483 1.70435276 [43] 2.67984044 -0.04976799 0.98716782 2.71171575 5.21648742 0.13860495 [49] 1.61038570 0.50679460
Use expand.grid to expand all arguments and create a data frame, and then use mapply. dat <- expand.grid(n = 10 * 1:5, mean = 1:4, sd = 1:3) mapply(rnorm, dat$n, dat$mean, dat$sd, SIMPLIFY = FALSE)
You can also use purrr::pmap() as an alternative to mapply library(purrr) dat <- expand.grid(n = 10 * 1:5, mean = 1:4, sd = 1:3) pmap(dat, rnorm)
Order colnames by char + number [duplicate]
This question already has answers here: How to sort a character vector where elements contain letters and numbers? (6 answers) Closed 7 years ago. I have dataframe with 223 columns. dput(colnames(a6)) c("d.54", "PRODUCT", "POS", "d.53", "d.52", "d.51", "d.50", "d.49", "d.48", "d.47", "d.46", "d.45", "d.44", "d.43", "d.42", "d.41", "d.40", "d.39", "d.38", "d.37", "d.36", "d.35", "d.34", "d.33", "d.32", "d.31", "d.30", "d.29", "d.28", "d.27", "d.26", "d.25", "d.24", "d.23", "d.22", "d.21", "d.20", "d.19", "d.18", "d.17", "d.16", "d.15", "d.14", "d.13", "d.12", "d.11", "d.10", "d.9", "d.8", "d.7", "d.6", "d.5", "d.4", "d.3", "d.2", "d.1", "d", "agr", "n", "s", "n.1", "s.1", "n.2", "s.2", "n.3", "s.3", "n.4", "s.4", "n.5", "s.5", "n.6", "s.6", "n.7", "s.7", "n.8", "s.8", "n.9", "s.9", "n.10", "s.10", "n.11", "s.11", "n.12", "s.12", "n.13", "s.13", "n.14", "s.14", "n.15", "s.15", "n.16", "s.16", "n.17", "s.17", "n.18", "s.18", "n.19", "s.19", "n.20", "s.20", "n.21", "s.21", "n.22", "s.22", "n.23", "s.23", "n.24", "s.24", "n.25", "s.25", "n.26", "s.26", "n.27", "s.27", "n.28", "s.28", "n.29", "s.29", "n.30", "s.30", "n.31", "s.31", "n.32", "s.32", "n.33", "s.33", "n.34", "s.34", "n.35", "s.35", "n.36", "s.36", "n.37", "s.37", "n.38", "s.38", "n.39", "s.39", "n.40", "s.40", "n.41", "s.41", "n.42", "s.42", "n.43", "s.43", "n.44", "s.44", "n.45", "s.45", "n.46", "s.46", "n.47", "s.47", "n.48", "s.48", "n.49", "s.49", "n.50", "s.50", "n.51", "s.51", "n.52", "s.52", "n.53", "s.53", "n.54", "s.54", "r.0", "r.1", "r.2", "r.3", "r.4", "r.5", "r.6", "r.7", "r.8", "r.9", "r.10", "r.11", "r.12", "r.13", "r.14", "r.15", "r.16", "r.17", "r.18", "r.19", "r.20", "r.21", "r.22", "r.23", "r.24", "r.25", "r.26", "r.27", "r.28", "r.29", "r.30", "r.31", "r.32", "r.33", "r.34", "r.35", "r.36", "r.37", "r.38", "r.39", "r.40", "r.41", "r.42", "r.43", "r.44", "r.45", "r.46", "r.47", "r.48", "r.49", "r.50", "r.51", "r.52", "r.53", "r.54") I try to reorder columns in such way agr d d.1 d.2 --d.54 .... sort by 1 char then sort by number in each group. I try a7=a6[,order(colnames(a6))] but it sort it like char only and a get such result colnames(a7) [1] "agr" "d" "d.1" "d.10" "d.11" "d.12" "d.13" "d.14" "d.15" "d.16" "d.17" [12] "d.18" "d.19" "d.2" "d.20" "d.21" Think there is simply answer on such question, but i cant find it...
You can use mixedorder from library(gtools) library(gtools) a6[mixedorder(nm1)] head(nm1[mixedorder(nm1)]) #[1] "agr" "d" "d.1" "d.2" "d.3" "d.4" Using another example set.seed(24) v1 <- sample(paste0(letters[1:4], '.', 1:20)) mixedsort(v1) #[1] "a.1" "a.5" "a.9" "a.13" "a.17" "b.2" "b.6" "b.10" "b.14" "b.18" #[11] "c.3" "c.7" "c.11" "c.15" "c.19" "d.4" "d.8" "d.12" "d.16" "d.20" data nm1 <- colnames(a6)
a6 <- setNames(do.call(data.frame,as.list(1:223)),c("d.54","PRODUCT","POS","d.53","d.52","d.51","d.50","d.49","d.48","d.47","d.46","d.45","d.44","d.43","d.42","d.41","d.40","d.39","d.38","d.37","d.36","d.35","d.34","d.33","d.32","d.31","d.30","d.29","d.28","d.27","d.26","d.25","d.24","d.23","d.22","d.21","d.20","d.19","d.18","d.17","d.16","d.15","d.14","d.13","d.12","d.11","d.10","d.9","d.8","d.7","d.6","d.5","d.4","d.3","d.2","d.1","d","agr","n","s","n.1","s.1","n.2","s.2","n.3","s.3","n.4","s.4","n.5","s.5","n.6","s.6","n.7","s.7","n.8","s.8","n.9","s.9","n.10","s.10","n.11","s.11","n.12","s.12","n.13","s.13","n.14","s.14","n.15","s.15","n.16","s.16","n.17","s.17","n.18","s.18","n.19","s.19","n.20","s.20","n.21","s.21","n.22","s.22","n.23","s.23","n.24","s.24","n.25","s.25","n.26","s.26","n.27","s.27","n.28","s.28","n.29","s.29","n.30","s.30","n.31","s.31","n.32","s.32","n.33","s.33","n.34","s.34","n.35","s.35","n.36","s.36","n.37","s.37","n.38","s.38","n.39","s.39","n.40","s.40","n.41","s.41","n.42","s.42","n.43","s.43","n.44","s.44","n.45","s.45","n.46","s.46","n.47","s.47","n.48","s.48","n.49","s.49","n.50","s.50","n.51","s.51","n.52","s.52","n.53","s.53","n.54","s.54","r.0","r.1","r.2","r.3","r.4","r.5","r.6","r.7","r.8","r.9","r.10","r.11","r.12","r.13","r.14","r.15","r.16","r.17","r.18","r.19","r.20","r.21","r.22","r.23","r.24","r.25","r.26","r.27","r.28","r.29","r.30","r.31","r.32","r.33","r.34","r.35","r.36","r.37","r.38","r.39","r.40","r.41","r.42","r.43","r.44","r.45","r.46","r.47","r.48","r.49","r.50","r.51","r.52","r.53","r.54")); names(a6)[do.call(order,c(read.table(text=names(a6),sep='.',fill=T),na.last=F))]; ## [1] "agr" "d" "d.1" "d.2" "d.3" "d.4" "d.5" ## [8] "d.6" "d.7" "d.8" "d.9" "d.10" "d.11" "d.12" ## [15] "d.13" "d.14" "d.15" "d.16" "d.17" "d.18" "d.19" ## [22] "d.20" "d.21" "d.22" "d.23" "d.24" "d.25" "d.26" ## [29] "d.27" "d.28" "d.29" "d.30" "d.31" "d.32" "d.33" ## [36] "d.34" "d.35" "d.36" "d.37" "d.38" "d.39" "d.40" ## [43] "d.41" "d.42" "d.43" "d.44" "d.45" "d.46" "d.47" ## [50] "d.48" "d.49" "d.50" "d.51" "d.52" "d.53" "d.54" ## [57] "n" "n.1" "n.2" "n.3" "n.4" "n.5" "n.6" ## [64] "n.7" "n.8" "n.9" "n.10" "n.11" "n.12" "n.13" ## [71] "n.14" "n.15" "n.16" "n.17" "n.18" "n.19" "n.20" ## [78] "n.21" "n.22" "n.23" "n.24" "n.25" "n.26" "n.27" ## [85] "n.28" "n.29" "n.30" "n.31" "n.32" "n.33" "n.34" ## [92] "n.35" "n.36" "n.37" "n.38" "n.39" "n.40" "n.41" ## [99] "n.42" "n.43" "n.44" "n.45" "n.46" "n.47" "n.48" ## [106] "n.49" "n.50" "n.51" "n.52" "n.53" "n.54" "POS" ## [113] "PRODUCT" "r.0" "r.1" "r.2" "r.3" "r.4" "r.5" ## [120] "r.6" "r.7" "r.8" "r.9" "r.10" "r.11" "r.12" ## [127] "r.13" "r.14" "r.15" "r.16" "r.17" "r.18" "r.19" ## [134] "r.20" "r.21" "r.22" "r.23" "r.24" "r.25" "r.26" ## [141] "r.27" "r.28" "r.29" "r.30" "r.31" "r.32" "r.33" ## [148] "r.34" "r.35" "r.36" "r.37" "r.38" "r.39" "r.40" ## [155] "r.41" "r.42" "r.43" "r.44" "r.45" "r.46" "r.47" ## [162] "r.48" "r.49" "r.50" "r.51" "r.52" "r.53" "r.54" ## [169] "s" "s.1" "s.2" "s.3" "s.4" "s.5" "s.6" ## [176] "s.7" "s.8" "s.9" "s.10" "s.11" "s.12" "s.13" ## [183] "s.14" "s.15" "s.16" "s.17" "s.18" "s.19" "s.20" ## [190] "s.21" "s.22" "s.23" "s.24" "s.25" "s.26" "s.27" ## [197] "s.28" "s.29" "s.30" "s.31" "s.32" "s.33" "s.34" ## [204] "s.35" "s.36" "s.37" "s.38" "s.39" "s.40" "s.41" ## [211] "s.42" "s.43" "s.44" "s.45" "s.46" "s.47" "s.48" ## [218] "s.49" "s.50" "s.51" "s.52" "s.53" "s.54"