I have a vector like this:
test <- c("a","b","c","d")
test
[1] "a" "b" "c" "d"
And I would like to paste a string, e.g. "_2", onto every other element of the vector, to get this:
"a" "b_2" "c" "d_2"
I tried this command:
ifelse(test %in% seq(1, length(test), 2), test, paste(test, "_2", sep=""))
but this just gives me:
"a_2" "b_2" "c_2" "d_2"
which is wrong. Any suggestions on how to properly do this? Thank you!
How about
paste0(c("a","b","c","d"), c("", "_2"))
[1] "a" "b_2" "c" "d_2"
Another option would be,
test[c(FALSE, TRUE)] <- paste0(test[c(FALSE, TRUE)], '_2')
test
#[1] "a" "b_2" "c" "d_2"
x <- c("a","b","c","d")
x[seq(2, length(x), by=2)] <- paste0(x[seq(2, length(x), by=2)], "_2")
x
this gives:
"a" "b_2" "c" "d_2"
Related
Suppose, I have a list:
l = list(c("a", "b", "c"), c("d", "e", "f"))
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "d" "e" "f"
I want to get a vector.
"ad" "be" "cf"
I can convert the list to a matrix, e.g.,sapply(l, c), and then concatenate columns, but, perhaps, there is an easier way.
We can use Reduce with paste0
Reduce(paste0, l)
[1] "ad" "be" "cf"
Or with do.call
do.call(paste0, l)
[1] "ad" "be" "cf"
Here is another option
> apply(list2DF(l), 1, paste0, collapse = "")
[1] "ad" "be" "cf"
I am trying to remove the text before and including a character ("-") for every element in a list.
Ex-
x = list(c("a-b","b-c","c-d"),c("a-b","e-f"))
desired output:
"b" "c" "d"
"b" "f"
I have tried using various combinations of lapply and gsub, such as
lapply(x,gsub,'.*-','',x)
but this just returns a null list-
[[1]]
[1] ""
[[2]]
[1] ""
And only using
gsub(".*-","",x)
returns
"d\")" "f\")"
You are close, but using lapply with gsub, R doesn't know which arguments are which. You just need to label the arguments explicitly.
x <- list(c("a-b","b-c","c-d"),c("a-b","e-f"))
lapply(x, gsub, pattern = "^.*-", replacement = "")
[[1]]
[1] "b" "c" "d"
[[2]]
[1] "b" "f"
This can be done with a for loop.
val<-list()
for(i in 1:length(x)){
val[[i]]<-gsub('.*-',"",x[[i]])}
val
[[1]]
[1] "b" "c" "d"
[[2]]
[1] "b" "f"
I guess this is better understood with an example, I feel this is really easy but I cannot get around it...
I have a list that looks like this:
[[1]] [1] "A" "B" "C" "D" "E" "F"
[[2]] [1] "A" "B" "C"
[[3]] [1] "A" "B" "C" "D"
[[4]] [1] "A" "B" "C" "D"
[[5]] [1] "A" "B" "C" "D" "E"
And I want to obtain this:
[[1]] [1] "A" "A;B" "A;B;C" "A;B;C;D" "A;B;C;D;E" "A;B;C;D;E;F"
[[2]] [1] "A" "A;B" "A;B;C"
[[3]] [1] "A" "A;B" "A;B;C" "A;B;C;D"
[[4]] [1] "A" "A;B" "A;B;C" "A;B;C;D"
[[5]] [1] "A" "A;B" "A;B;C" "A;B;C;D" "A;B;C;D;E"
So I need a function to apply in this way:
list2 <- lapply(list1,
function(x) {
#something here
})
We can loop through the list, get the sequence of the length of elements, loop through it with sapply, extract the list elements based on the index and paste
lapply(list1, function(x) sapply(seq(length(x)),
function(i) paste(x[seq_len(i)], collapse=",")))
#[[1]]
#[1] "A" "A,B" "A,B,C" "A,B,C,D" "A,B,C,D,E" "A,B,C,D,E,F"
#[[2]]
#[1] "A" "A,B" "A,B,C"
#[[3]]
#[1] "A" "A,B" "A,B,C" "A,B,C,D"
#[[4]]
#[1] "A" "A,B" "A,B,C" "A,B,C,D"
#[[5]]
#[1] "A" "A,B" "A,B,C" "A,B,C,D" "A,B,C,D,E"
Or another option is Reduce with accumulate = TRUE
lapply(list1, function(x) Reduce(function(...) paste(..., sep=","), x, accumulate = TRUE))
This can be written without an anonymous function call if the sep is not important
lapply(list1, Reduce, f = paste, accumulate = TRUE)
data
list1 <- lapply(c(6, 3, 4, 4, 5), function(i) LETTERS[1:i])
I have a list of elements (letter here in the example)
(l <- list(letters[1:2], letters[2:3]))
# [[1]]
# [1] "a" "b"
# [[2]]
# [1] "b" "c"
And another elements
(r <- letters[2])
# [1] "b"
The R function must delete evrything before "b" and "b" itself.
So the result will be like this :
# [[1]]
# [1] "c"
Any idea please?
Thank you in advance
Try
out = lapply(l, function(x) x[-c(1,which(x == "b"))])
Filter(length, out)
#[[1]]
#[1] "c"
or as #akrun suggested
Filter(length,lapply(l, function(x) x[-seq(match("b",x))]))
I have several vectors:
aa<-c("a","b","b","b",NA)
bb<-c("g","g","g","i",NA)
cc<-c("y","y","x","y",NA)
all<-c("aa","bb","cc")
I wrote a loop so that all NA will be replaced by the most frequent levels:
for (i in 1:3)
{
get(all[i])[is.na(get(all[i]))]<-names(which.max(table(get(all[i]))))
}
But it doesn't work? Can someone explain why? I suspect it's something to do with the get() function?
Thank you
Try:
lst1 <- lapply(mget(all),function(x) {x[is.na(x)] <-names(which.max(table(x)))
x})
lst1
# $aa
#[1] "a" "b" "b" "b" "b"
# $bb
#[1] "g" "g" "g" "i" "g"
#$cc
#[1] "y" "y" "x" "y" "y"
In case, you wanted to replace the NA in original variable
list2env(lst1, envir=.GlobalEnv)
aa
#[1] "a" "b" "b" "b" "b"