List A1 structured like this:
#[[1]]
[1] "12" "1"
#[[2]]
[1] "13" "1"
#[[3]]
[1] "14" "2"
#[[4]]
[1] "15" "2"
#[[5]]
[1] "16" "1"
#[[6]]
[1] "18" "2"
#[[7]]
[1] "20" "0"
#[[8]]
[1] "21" "2"
#[[9]]
[1] "21" "4"
#[[10]]
[1] "34" "1" "0"
#[[11]]
[1] "42" "1" "1"
#[[12]]
[1] "42" "2" "2"
and I’m trying to extract the unique values in the first item of each list element (for example, the number 12 in [[1]]).
When I use
v <- unique(gsub( " .*$", "", A1))
the resulting vector v looks like
"c("12"," "c("13"," "c("14"," "c("15"," "c("16"," "c("18"," "c("20"," "c("21","
"c("34"," "c("42","
The desired result would be (12, 13, 14, 15, 16, 17, 18, 20, 21, 34, 42)
How do I get rid of the additional characters, where do they come from?
Thank you in advance!
>Solution :
You can use
v <- list(c("12", "1"), c("13", "1"), c("12", "3"))
unique(sapply(v, "[[", 1))
# => [1] "12" "13"
See the R demo online.
Note:
sapply(v, "[[", 1)– gets the first itemsuniqueleaves only the unique values.