How to delete elements of a list in R?


Paio

I have an igraph object which I created with the igraph library. This object is a list. Some components of this list have length 2. I want to remove all these components.

IGRAPH clustering walktrap, groups: 114, mod: 0.79
+ groups:
  $`1`
     [1] "OTU0041"             "OTU0016"             "OTU0062"            
     [4] "OTU1362"             "UniRef90_A0A075FHQ0" "UniRef90_A0A075FSE2"
     [7] "UniRef90_A0A075FTT8" "UniRef90_A0A075FYU2" "UniRef90_A0A075G543"
    [10] "UniRef90_A0A075G6B2" "UniRef90_A0A075GIL8" "UniRef90_A0A075GR85"
    [13] "UniRef90_A0A075H910" "UniRef90_A0A075HTF5" "UniRef90_A0A075IFG0"
    [16] "UniRef90_A0A0C1R539" "UniRef90_A0A0C1R6X4" "UniRef90_A0A0C1R985"
    [19] "UniRef90_A0A0C1RCN7" "UniRef90_A0A0C1RE67" "UniRef90_A0A0C1RFI5"
    [22] "UniRef90_A0A0C1RFN8" "UniRef90_A0A0C1RGE0" "UniRef90_A0A0C1RGX0"
    [25] "UniRef90_A0A0C1RHM1" "UniRef90_A0A0C1RHR5" "UniRef90_A0A0C1RHZ4"
  + ... omitted several groups/vertices

For example, this:

> a[[91]]
[1] "OTU0099"                "UniRef90_UPI0005B28A7E"

I tried this but it doesn't work:

a[lapply(a,length)>2]

Any help?

Pavo Diving

Since you didn't provide any reproducible data or examples, I had to generate some dummy data:

# create dummy data
a <- list(x = 1, y = 1:4, z = 1:2)

# remove elements in list with lengths greater than 2:
a[which(lapply(a, length) > 2)] <- NULL

If you want to remove items whose length is exactly 2 (the question is unclear), the last line should be replaced with:

a[which(lapply(a, length) == 2)] <- NULL

Related


How to delete elements of a list in R?

Paio I have an igraph object which I created with the igraph library. This object is a list. Some components of this list have length 2. I want to remove all these components. IGRAPH clustering walktrap, groups: 114, mod: 0.79 + groups: $`1` [1] "OTU004

How to delete elements in a list using delete order?

Ding medicine I have a list like this lst = [1,3,5]and a main list like thislost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ] I want to remove elements in the second list based on the index in the first list. This means I have to remove 'cat', 'you'

delete list conditional on number of elements in R

coronary heart disease I have an Lunnamed list of comma separated characters. Each character list is not of equal length. I need to delete character lists with less than 4 character lists L. How can this be done? Example L: > L [[1]] [1] "A" "B" "C" "D" [[2]]

delete list conditional on number of elements in R

coronary heart disease I have an Lunnamed list of comma separated characters. Each character list is not of equal length. I need to delete character lists with less than 4 character lists L. How can this be done? Example L: > L [[1]] [1] "A" "B" "C" "D" [[2]]

delete list conditional on number of elements in R

coronary heart disease I have an Lunnamed list of comma separated characters. Each character list is not of equal length. I need to delete character lists with less than 4 character lists L. How can this be done? Example L: > L [[1]] [1] "A" "B" "C" "D" [[2]]

delete list conditional on number of elements in R

coronary heart disease I have an Lunnamed list of comma separated characters. Each character list is not of equal length. I need to delete character lists with less than 4 character lists L. How can this be done? Example L: > L [[1]] [1] "A" "B" "C" "D" [[2]]

delete list conditional on number of elements in R

coronary heart disease I have an Lunnamed list of comma separated characters. Each character list is not of equal length. I need to delete character lists with less than 4 character lists L. How can this be done? Example L: > L [[1]] [1] "A" "B" "C" "D" [[2]]

How to reference .. .. elements in a list in R?

Maxther How can I access list elements that start with a double dot .. ..? I'm trying to isolate the $attr part of the html to help convert another question into a variable and then place it as another mime variable. x$parts$attr gives me NULL. thank you for y

How to extract elements of a list in R?

Paillou I have a list with several vectors that looks like this: $`56` [1] "OTU2998" "UniRef90_A0A1Z9FS94" "UniRef90_A0A257ESC3" [4] "UniRef90_A0A293NAV3" "UniRef90_A0A2E1NMU8" "UniRef90_A0A2E1NPX9" [7] "UniRef90_A0A2E1NQL1" "UniRef90_A0A2E1NRD2" "

How to reference .. .. elements in a list in R?

Maxther How can I access list elements that start with a double dot .. ..? I'm trying to isolate the $attr part of the html to help turn another question into a variable and then place it as another mime variable. x$parts$attr gives me NULL. thank you for your

How to extract elements of a list in R?

Paillou I have a list with several vectors that looks like this: $`56` [1] "OTU2998" "UniRef90_A0A1Z9FS94" "UniRef90_A0A257ESC3" [4] "UniRef90_A0A293NAV3" "UniRef90_A0A2E1NMU8" "UniRef90_A0A2E1NPX9" [7] "UniRef90_A0A2E1NQL1" "UniRef90_A0A2E1NRD2" "

How to access list elements in R?

Anis Bakir I have a list called change_points consisting of 6 rows, each row has 1 or 2 elements. It looks like this: My question is, how can I access 654 from the second row? or any single value or any row. I've searched SO and all I can figure out is that sa

How to quickly append elements of a list to a list in R?

iamsoconfused12321 Here is my code snippet: m <- as.data.frame.matrix(matrix(c(20, 32, 52, 84, 98, 101), ncol = 2, nrow = 3)) ages <- as.numeric() for(i in 1:nrow(m)){ ages <- c(ages, c(m$V1[i]:m$V2[i])) } Essentially, the first column is the start age and

How to quickly append elements of a list to a list in R?

iamsoconfused12321 Here is my code snippet: m <- as.data.frame.matrix(matrix(c(20, 32, 52, 84, 98, 101), ncol = 2, nrow = 3)) ages <- as.numeric() for(i in 1:nrow(m)){ ages <- c(ages, c(m$V1[i]:m$V2[i])) } Essentially, the first column is the start age and

Rotate and delete elements in a list

Graziko I want to iterate through list A and remove from that list any element with the same key as the key pair located in list B, then create a new list C from it. I get that it only works for the head, but I don't know how to loop through the tail of list B

Rotate and delete elements in a list

Graziko I want to iterate through list A and remove from that list any element with the same key as the key pair located in list B, then create a new list C from it. I get that it only works for the head, but I don't know how to loop through the tail of list B

R delete rows of list elements and keep row names

Annemarie I have a dataframe with empty cells that I split into a list: df <- data.frame(c("q","w","","r","t","y"),c("a","b","","d","e","f"),c("x","c","v","b","","m")) colnames(df) <- c("qwerty","abc","bnm") rownames(df) <- c("1a","1b","1c","1d","1e","1f") lis

R delete rows of list elements and keep row names

Annemarie I have a dataframe with empty cells that I split into a list: df <- data.frame(c("q","w","","r","t","y"),c("a","b","","d","e","f"),c("x","c","v","b","","m")) colnames(df) <- c("qwerty","abc","bnm") rownames(df) <- c("1a","1b","1c","1d","1e","1f") lis

R delete rows of list elements and keep row names

Anne Marie I have a dataframe with empty cells that I split into a list: df <- data.frame(c("q","w","","r","t","y"),c("a","b","","d","e","f"),c("x","c","v","b","","m")) colnames(df) <- c("qwerty","abc","bnm") rownames(df) <- c("1a","1b","1c","1d","1e","1f") li

R delete rows of list elements and keep row names

Anne Marie I have a dataframe with empty cells that I split into a list: df <- data.frame(c("q","w","","r","t","y"),c("a","b","","d","e","f"),c("x","c","v","b","","m")) colnames(df) <- c("qwerty","abc","bnm") rownames(df) <- c("1a","1b","1c","1d","1e","1f") li

How can I delete rows for those column elements of a list?

use In the image above, the last column ("Location") is a list. I need to delete these lines for the following cases: if first item in the list 'location' is greater than 60.0, those rows are not needed for me If I use: for i in range(len(output)): trip_d

C++ how to delete list elements by user input

Thomas Pavel Norwich In the program after the array is created, the user can select and delete an element from the array. After that, the program will display the array without this element. One requirement for this assignment is the use of pointers. using nam