0

I have a list of lists like:

List_PatientState
    [[1]]
          BirthYear Hispanic tEye tStatus tKidney         
    12604         2        0    1       2       2 

    [[2]]
         BirthYear Hispanic tEye tStatus tKidney          
    9252         2        0    2       1       1 

    [[3]]
         BirthYear Hispanic tEye tStatus tKidney          
    6613         2        0    1       1       1 

    [[4]]
         BirthYear Hispanic tEye tStatus tKidney        
    6265         2        0    2       2       1 

    [[5]]
         BirthYear Hispanic tEye tStatus tKidney          
    6202         2        0    1       1       2 

in a loop I create a new list and I would like to neglect the list if it is available in my current list of list

for more information, consider this list that is created in my loop:

[[6]]
      BirthYear Hispanic tEye tStatus tKidney          
11773   2        0    1       1       1 

This is similar to:

 List_PatientState[[3]]

I would like to detect such lists ,in my list of lists,So, I could reject it to add in my current list of lists. Is there anybody to help me. I would be much appreciated. the output of dput(List_PatientState):

> dput(List_PatientState)
list(structure(list(BirthYear = structure(2L, .Label = c("1", 
"2", "3"), class = "factor"), Hispanic = structure(1L, .Label = c("0", 
"1"), class = "factor"), tEye = structure(1L, .Label = c("1", 
"2"), class = "factor"), tStatus = structure(2L, .Label = c("1", 
"2"), class = "factor"), tKidney = structure(2L, .Label = c("1", 
"2"), class = "factor")), .Names = c("BirthYear", "Hispanic", 
"tEye", "tStatus", "tKidney"), row.names = 12604L, class = "data.frame"), 
    structure(list(BirthYear = structure(2L, .Label = c("1", 
    "2", "3"), class = "factor"), Hispanic = structure(1L, .Label = c("0", 
    "1"), class = "factor"), tEye = structure(2L, .Label = c("1", 
    "2"), class = "factor"), tStatus = structure(1L, .Label = c("1", 
    "2"), class = "factor"), tKidney = structure(1L, .Label = c("1", 
    "2"), class = "factor")), .Names = c("BirthYear", "Hispanic", 
    "tEye", "tStatus", "tKidney"), row.names = 9252L, class = "data.frame"))

2 Answers2

0

First we create some sample data:

dat <- lapply(1:5,function(x) mtcars[x,]) #List of the first 5 rows of mtcars set

Then, if you have to use a for loop:

for (i in 3:6) {
   temp <- mtcars[i,]
   if(is.na(any(sapply(dat,function(y) all.equal(y,temp)[[1]])))) {
    dat[[i]] <- temp
 }
}

Basically checks in every Iteration if the data frame you create in the Loop already exists in your list and only adds data frames that are not contained in your list.

count
  • 1,328
  • 9
  • 16
  • Thank you so much for your response at first. but this part "all.equal(y,temp))[[1]]" shouldn't be "all.equal(y,temp))[[i]]"? I do not understand it. – Somayeh Ghazalbash Aug 29 '17 at 09:25
  • You are right, edited. Its better to use `sapply` to return a vector instead of a list, also, the bracket squares should fetch the first elelement of the `all.equal` comparison. – count Aug 29 '17 at 09:33
0

Your data

A list of vectors

List_PatientState <- list(c(Birthyear=2, Hispanic=0, tEye=1, tStatus=2, tKidney=2),
                          c(Birthyear=2, Hispanic=0, tEye=2, tStatus=1, tKidney=1),
                          c(Birthyear=2, Hispanic=0, tEye=1, tStatus=1, tKidney=1), 
                          c(Birthyear=2, Hispanic=0, tEye=2, tStatus=2, tKidney=1),
                          c(Birthyear=2, Hispanic=0, tEye=1, tStatus=1, tKidney=2))

New vectors to potentially add

newv <- list(c(Birthyear=2, Hispanic=0, tEye=1, tStatus=1, tKidney=1))     # duplicate values
newv1 <- list(c(Birthyear=2, Hispanic=0, tEye=2, tStatus=1, tKidney=2))    # unique values

Function to append if unique values

myfun <- function(L, V) {
              if (any(sapply(L, function(x) all(V[[1]] == x)))) {
                    return(L)
              } else {
                    return(c(L,V))
              }
         }

Tests

myfun(List_PatientState, newv)    # returns original list
myfun(List_PatientState, newv1)   # returns original list + newv1
CPak
  • 13,260
  • 3
  • 30
  • 48