How do I get a unique named list? - r

This is a simple question and I think I can probably re-invent the wheel and write something custom but I'm sure there must be an easy way to do this that I can't think of at the moment. Suppose I have a list:
l <- list("NY"=10001, "CT"=10002, "CT"=10002)
I would like a list:
list("NY"=10001, "CT"=10002)
I tried to use unique(l) but it just returns:
list(10001, 10002)
How do I get a unique list but preserve the names assigned to the values?

Using duplicated:
l[ !duplicated(l) ]

Given that
Each string is mapped to 1 number
we can do:
l[unique(names(l))]
Edit:, another alternative
tapply(l, names(l), `[`, 1)

Try the duplicated function:
l=list("NY"=10001, "CT"=10002, "CT"=10002)
l[!duplicated(l)]
Results in:
$NY
[1] 10001
$CT
[1] 10002

Related

See the attributes of a dataframe but exclude rownames

I'd like to see the attributes of a dataframe but exclude the $row.names part.
I'm using attributes().
attributes( df )
Is there a way to achieve this using this function or do I need to seek a different function?
Thanks in advance.
We could use
atr1 <- attributes(df)
atr1[setdiff(names(atr1), "row.names")]
If we want in a single step, use modifyList
modifyList(attributes(df), list("row.names" = NULL))

How to name the elements of an unnamed list

I have a list that look like this:
setlist2 <- list(wsb_b6, wsb_id8)
[[1]]
[1] "Gm10116" "Tpm3-rs7" "Wdfy1" "Rps3a2" "AK157302" "Gm6563"
"Gm9825" "Gm10259" "Gm6768"
[[2]]
[1] "Gm6401" "Ecel1" "Hpca" "Tmem176a" "Lepr"
"Baiap3" "Fam183b" "Vsx2" "Vtn"
I need it to look like this:
$wsb_b6
[1] "Gm10116" "Tpm3-rs7" "Wdfy1" "Rps3a2" "AK157302" "Gm6563"
"Gm9825" "Gm10259" "Gm6768"
$wsb_id8
[1] "Gm6401" "Ecel1" "Hpca" "Tmem176a" "Lepr"
"Baiap3" "Fam183b" "Vsx2" "Vtn"
I know that by doing it manually I can achieve it but it is more that 100 each, there's got to be a better way
#I found that I had to unlist my two previous lists
wsb_b6 <-wsb_b6[,1]
wsb_b6 <-unlist(wsb_b6)
wsb_id8 <-wsb_id8[,1]
wsb_id8 <- unlist(wsb_id8)
#And then list them again, but like this
setlist2 <-list(wsb_b6=wsb_b6, wsb_id8= wsb_id8)
Use dplyr::lst
setlist2 <- dplyr::lst(wsb_b6, wsb_id8)
It sounds like you want to create a named list. Specifically, you want to create a named list where the names are taken from the names of the variables in the environment.
This is similar to this question: Can lists be created that name themselves based on input object names?
I don't believe there is a simple function to do this in base R, but you can using the function llist from the package Hmisc:
library(Hmisc)
setlist2 <- llist(wsb_b6, wsb_id8)

Use lists/dataframes as items in for-loops in R

I am quite sure this is basic stuff, but I just can't find the answer by googling. So my problem:
I want to use a for-loop on a list of lists or data frames. But when you use list[i], you get all the values in the data frame instead of the data frame it self. Can anyone point out to me how to code this properly?
Example of the code:
a<-data.frame(seq(1:3),seq(3:1))
b<-data.frame(seq(1:3),seq(3:1))
l<-c(a,b)
Then l[1] returns:
> l[1]
$seq.1.3..
[1] 1 2 3
And I want it to just return: a
You can use the list function:
a<-data.frame(1:3,1:3)
b<-data.frame(3:1,3:1)
l<-list(a,b)
And access it's value with double brackets [[:
l[[1]]
l[[2]]
Ps: seq(1:3) and seq(3:1) outputs the same value, so I used 1:3 and 3:1. :)

R: performing same task on multiple matrices

I have about 150 matrices, each with a name in the convention of "BID_xxx" (for example: BID_ABL, BID_BGA). I would like to split the first column of each of these matrices into two using substr. So, for example: BID_ABL[,5] = substr(BID_ABL[,1],1,10)
Would anyone be able to help me find a way of doing this without writing out the above line 150 times, once for each matrix?
Any help would be great!
Thanks
Mike
The functions get and assign are your friends here:
for (n in ls()[grep("^BID_",ls())]) {
x <- get(n)
x[,5] <- substr(x[,1],1,10)
assign(n, x)
}
Should do what you want.
Like this:
allnames<- ls(pat='BID_')
for(j in 1:length(allnames)) print(get(allnames[j])[1])
Where you'd replace "print" with your substring function.
Edit: Sam's answer is essentially the same. How you get the list of object names depends on what other stuff is in your environment.

how do I get the difference between two R named lists?

OK, I've got two named lists, one is "expected" and one is "observed". They may be complex in structure, with arbitrary data types. I want to get a new list containing just those elements of the observed list that are different from what's in the expected list. Here's an example:
Lexp <- list(a=1, b="two", c=list(3, "four"))
Lobs <- list(a=1, c=list(3, "four"), b="ni")
Lwant <- list(b="ni")
Lwant is what I want the result to be. I tried this:
> setdiff(Lobs, Lexp)
[[1]]
[1] "ni"
Nope, that loses the name, and I don't think setdiff pays attention to the names. Order clearly doesn't matter here, and I don't want a=1 to match with b=1.
Not sure what a good approach is... Something that loops over a list of names(Lobs)? Sounds clumsy and non-R-like, although workable... Got any elegant ideas?
At least in this case
Lobs[!(Lobs %in% Lexp)]
gives you what you want.
OK, I found one slightly obtuse answer, using the plyr package:
> Lobs[laply(names(Lobs), function(x) !identical(Lobs[[x]], Lexp[[x]]))]
$b
[1] "ni"
So, it takes the names of the array from the observed function, uses double-bracket indexing and the identical() function to compare the sub-lists, then uses the binary array that results from laply() to index into the original observed function.
Anyone got a better/cleaner/sexier/faster way?

Resources