combining elements in a list of lists in R - r

I have a list called samples_ID with 116 vectors, each vectors has three elements like these:
"11" "GT20-16829" "S27"
I wanna keep the 116 vectors, but combine the elements to a single element like this
"11_GT20-16829_S27"
I tried something like this
samples_ID_ <- paste(samples_ID, collapse = "_")
it returns a single vector, below is just a part of it:
..._c(\"33\", \"GT20-16846\", \"S24\")_c(\"33\", \"GT20-18142\", \"S72\")_c(\"34\", \"GT20-16819\", \"S50\")_c...
What am I doing wrong?
Can you help me please?
Thanks

A tidyverse option.
library(stringr)
library(purrr)
map(samples_ID, ~ str_c(., collapse = '_'))
# [[1]]
# [1] "11_GT20-16829_S27"
#
# [[2]]
# [1] "12_GT20-16830_S28"
Data
samples_ID <- list(c("11", "GT20-16829", "S27"), c("12", "GT20-16830", "S28"
))

In base R, we can use sapply
sapply(samples_ID, paste, collapse="_")

Another base R option using paste
do.call(paste, c(data.frame(t(list2DF(samples_ID))), sep = "_"))
or
do.call(paste, data.frame(do.call(rbind, samples_ID)), sep = "_"))

Related

string split and interchange the position of string in R

I have a vector called myvec. I would like to split it at _ and interchange the position. What would be the simplest way to do this?
myvec <- c("08AD09144_NACC022453", "08AD8245_NACC657970")
Result I want:
NACC022453_08AD09144, NACC657970_08AD8245
You can do this with regex capturing data in two groups and interchanging them using back reference.
myvec <- c("A1_B1", "B2_C1", "D1_A2")
sub('(\\w+)_(\\w+)', '\\2_\\1', myvec)
#[1] "B1_A1" "C1_B2" "A2_D1"
We can use strsplit from base R
sapply(strsplit(myvec, "_"), function(x) paste(x[2], x[1], sep = "_"))
#[1] "NACC022453_08AD09144" "NACC657970_08AD8245"

Newbie on R and newbie to programmation - fusion all values of a vector into one some value

It might be a dumb question but I'm very new to programming and I'm currently working with R
I want to transform a vector A = "AT" "GCT" "TCA" into A ="ATGCTTCA".
Can someone help me please?
We can use str_c from stringr
library(stringr)
str_c(A, collapse="")
#[1] "ATGCTTCA"
Or with paste from base R
paste(A, collapse="")
#[1] "ATGCTTCA"
data
A <- c("AT", "GCT", "TCA")
Try this using paste0() over your vector:
#Data
A = c("AT","GCT","TCA")
#Collapse
Anew <- paste0(A,collapse = '')
Output:
[1] "ATGCTTCA"

How to reformat column in place using strsplit()

In my r dataframe, I have a column that looks like this:
df$Year
"Cumulative.12.2013.Actual"
"Cumulative.12.2014.Actual"
"Cumulative.12.2015.Actual"
"Cumulative.12.2016.Actual"
"Cumulative.12.2017.Actual"
"Cumulative.12.2018.Actual"
"Cumulative.12.2019.Actual"
"Cumulative.5.2020.Actual"
I'm trying to re-format the column such that I only include the dates. It should look like:
df$Year
"12/2013"
"12/2014"
"12/2015"
"12/2016"
"12/2017"
"12/2018"
"12/2019"
"5/2020"
How can I achieve this? I tried doing it all in one line, but all it returns in df$Year is "12/2013" for all the rows :
df$Year< - paste(strsplit(df$Year, ".", fixed=TRUE)[[1]][2], strsplit(x, ".", fixed=TRUE)[[1]][3], sep="/")
Here is an option.
df$Year %<>%
as.character %>%
strsplit(.,"([a-zA-Z]\\.)|(\\.[a-zA-Z])") %>%
sapply( .,function(i) i[2] %>%
gsub(".","/",.,fixed=T))
I wouldn't use strsplit for this. It would be better to use something like gsub.
gsub("^[^0-9]+(\\d+).(\\d+)[^0-9]+$", "\\1/\\2", x)
## [1] "12/2013" "12/2014" "12/2015" "12/2016" "12/2017" "12/2018" "12/2019" "5/2020"
There are many other alternatives. For example this is a hack I picked up from #akrun that would be useful here (and probably the fastest, given the nature of the data):
sub(".", "/", trimws(x, whitespace = "[^0-9]"), fixed = TRUE)
## [1] "12/2013" "12/2014" "12/2015" "12/2016" "12/2017" "12/2018" "12/2019" "5/2020"
Or you could do this:
sub(".", "/", gsub("Cumulative.|.Actual", "", x), fixed = TRUE)
## [1] "12/2013" "12/2014" "12/2015" "12/2016" "12/2017" "12/2018" "12/2019" "5/2020"
If you didn't need the forward slash for the date, you could even do something like this instead:
gsub("Cumulative.|.Actual", "", x)
## [1] "12.2013" "12.2014" "12.2015" "12.2016" "12.2017" "12.2018" "12.2019" "5.2020"
Sample data:
x <- c("Cumulative.12.2013.Actual", "Cumulative.12.2014.Actual", "Cumulative.12.2015.Actual",
"Cumulative.12.2016.Actual", "Cumulative.12.2017.Actual", "Cumulative.12.2018.Actual",
"Cumulative.12.2019.Actual", "Cumulative.5.2020.Actual")

How to drop substring from variable names?

I have the following names of variables:
vars <- c("var1.caps(12, For]","var2(5,For]","var3.tree.(15, For]","var4.caps")
I need to clean these names in order to get the following result:
clean_vars <- c("var1.caps","var2","var3.tree.","var4.caps")
So, basically I would like to drop (..].
Is there any automated way to do it in R?
I was trying to adapt str_replace(vars, pattern, ""), but not sure how to make pattern flexible because it could have different values between ( and ].
gsub("\\(.*\\]","",vars)
[1] "var1.caps" "var2" "var3.tree." "var4.caps"
Using stringr and purrr:
stringr::str_split(vars, "\\(") %>% purrr::map(., 1) %>% unlist()
[1] "var1.caps" "var2" "var3.tree." "var4.caps"
Another option of using gsub
> gsub("(?<=)\\(.*\\]","\\1",vars,perl = T)
[1] "var1.caps" "var2" "var3.tree."
[4] "var4.caps
Eliminate
the first ( (in regex \\() in the string
and everything that comes after it (.+).
Replace it with nothing ("").
sub("\\(.+", "", vars)
# [1] "var1.caps" "var2" "var3.tree." "var4.caps"

R combine list items

I have a R list as following
mlist <- list(name = c('id','value'), type = c('bigint','float'))
I want to combine it in a way which I can end up with the following string
id bigint,value float
I searched but could not find a way to do that. Can someone let me know how can I do that without looping over rows, I want to be able to use something like apply function
With purrr, we can also do:
library(purrr)
toString(pmap(mlist, paste))
# [1] "id bigint, value float"
Another Base R approach:
toString(Reduce(function(x1, x2){
mapply(function(x2, y2){
paste(x2, y2, collapse = " ")
}, x1, x2)
}, mlist))
# [1] "id bigint, value float"
We can use Map
do.call(Map, c(f = c, unname(mlist)))
#$id
#[1] "id" "bigint"
#$value
#[1] "value" "float"
If it needs to be a single string, use paste
do.call(Map, c(f = paste, unname(mlist)))
If we need to get a vector as output use unlist
unlist(do.call(Map, c(f = paste, sep="_", unname(mlist))), use.names = FALSE)
#[1] "id_bigint" "value_float"
Or in tidyverse
library(purrr)
transpose(mlist) %>%
map(flatten_chr)
#[[1]]
#[1] "id" "bigint"
#[[2]]
#[1] "value" "float"
Similar to #avid_useR
library(purrr)
pmap_chr(mlist, paste)

Resources