joining lists of different length in R - r

I am not sure if R has the capabilities to do this, but I'd like to join two different lists of different lengths so that it's like a nested list within a list (if that makes sense).
edit: I'd like to add values in x as an additional value in z.
z <- c("a", "b", "c")
x <- c("c", "g")
c(z, x)
[1] "a" "b" "c" "c" "g"
# what I'd really like to see
[1] "a" "b" "c" "c, g"
I think it would be something similar to doing the following in python pandas
self.z.append(x)

We can paste the 'x' together and concatenate with 'z'
c(z, toString(x))
#[1] "a" "b" "c" "c, g"

Related

how to change a vector to corresponding names without using for loop

I have a vector c(1,3,4,2,5,4,3,1,6,3,1,4,2), and I want make 1="a", 2="b", and so on
So my final outputs should look like c(a,c,d,b...)
I know that I can use for loop and if statement to do this, but is there any other quicker ways to do?
You may use the built-in constant letters.
vec <- c(1,3,4,2,5,4,3,1,6,3,1,4,2)
res <- letters[vec]
res
#[1] "a" "c" "d" "b" "e" "d" "c" "a" "f" "c" "a" "d" "b"
To replace with any other values you can construct a vector to subset.
value <- c('apples', 'banana', 'grapes', .....)
res <- value[vec]
We may use match
letters[match(vec, unique(vec))]

Using sapply to list all parts of a vector, but not show the list on the bottom

I have a bit of code, lets say
test<-c("A", "B", "C") that I want to list individually line by line. I use sapply(test, FUN=print) and I get
[1] "A"
[1] "B"
[1] "C"
A B C
"A" "B" "C"
but I want to get this instead. Is there any way to do so using any of the apply functions?
[1] "A"
[1] "B"
[1] "C"
I have been able to do this using a for loop
for (i in 1:length(test)){
print(test[i])
}
but I'm trying to do this with the apply family specifically.
We can use invisible to wrap to that it won't print the output from sapply
invisible(sapply(test, FUN=print))
#[1] "A"
#[1] "B"
#[1] "C"
here is a different approach
cat(paste0(test,collapse="\n"))
A
B
C

Remove duplicates in a nested list

I have a large list of lists where I want to remove duplicated elements in each list. Example:
x <- list(c("A", "A", "B", "C"), c("O", "C", "A", "Z", "O"))
x
[[1]]
[1] "A" "A" "B" "C"
[[2]]
[1] "O" "C" "A" "Z" "O"
I want the result to be a list that looks like this, where duplicates within a list are removed, but the structure of the list remains.
[[1]]
[1] "A" "B" "C"
[[2]]
[1] "O" "C" "A" "Z"
My main strategy has been to use rapply (also tried lapply) to identify duplicates and remove them. I tried:
x[rapply(x, duplicated) == T]
but received the following error:
"Error: (list) object cannot be coerced to type 'logical'"
Does anyone know a way to solve this issue?
Thanks!
We can use lapply with unique
lapply(x, unique)
#[[1]]
#[1] "A" "B" "C"
#[[2]]
#[1] "O" "C" "A" "Z"
The issue with rapply, is that it recursively applies the duplicated and then returns a single vector instead of a list of logical vectors
rapply(x, duplicated)
#[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
Instead it can be
lapply(x, function(u) u[!duplicated(u)])
#[[1]]
#[1] "A" "B" "C"
#[[2]]
#[1] "O" "C" "A" "Z"

R - List manipulation element concatenation

Assume I have a list with 5 elements:
list <- list("A", "B", "C", "D", c("E", "F"))
I am trying to return this to a simple character vector using purrr with the need to combine list elements that have two strings into one, separated by a delimiter such as '-'. The output should look like this:
chr [1:5] "A" "B" "C" "D" "E-F"
I've tried a ton of approaches including paste, paste0, str_c and where I am getting hung up is it seems that map applies the function to each individual string of an element of a list and not the group of strings of an element (when there are more than one). The closes I've gotten is:
list2 <- unlist(map(list, str_flatten))
str(list2)
This returns:
chr [1:5] "A" "B" "C" "D" "EF"
where I need a hyphen between E and F:
chr [1:5] "A" "B" "C" "D" "E-F"
When I try to pass a function as a parenthetiinton to str_flatten(), such as str_flatten(list, collapse = "-"), it doesn't work. The big problem is I can't figure out what string to pass as an argument in str_flatten to group two strings of a given element of a list.
You almost had it. Try
library(purrr)
library(stringr)
unlist(map(lst, str_flatten, collapse = "-"))
#[1] "A" "B" "C" "D" "E-F"
You could also use map_chr
map_chr(lst, str_flatten, collapse = "-")
Without additional packages and with thanks to #G.Grothendieck you could do
sapply(lst, paste, collapse = "-")
data
lst <- list("A", "B", "C", "D", c("E", "F"))
We can also use map_chr and paste.
library(purrr)
lst <- list("A", "B", "C", "D", c("E", "F"))
map_chr(lst, ~paste(.x, collapse = "-"))
# [1] "A" "B" "C" "D" "E-F"

Delete list conditional on the number of elements in R

I have a list L of unnamed comma separated character lists. Each list of characters is of unequal length. I need to drop the character lists that have less than 4 elements from L. How can this be done? Example L:
> L
[[1]]
[1] "A" "B" "C" "D"
[[2]]
[1] "E" "F" "G"
In the example above I would like to end up with:
> L
[[1]]
[1] "A" "B" "C" "D"
We can use lengths to get the length of the list elements as a vector, create a logical vector based on that and subset the list
L[lengths(L)>3]
#[[1]]
#[1] "A" "B" "C" "D"
A less optimized approach (used earlier) is to loop through the list elements with sapply, get the length and use that to subset
L[sapply(L, length)>3]
data
L <- list(LETTERS[1:4], LETTERS[5:7])

Resources