Se non si desidera modificare l'elenco sul posto (ad esempio per passare l'elenco con un elemento rimosso in una funzione), è possibile utilizzare l'indicizzazione: gli indici negativi indicano "non includere questo elemento".
x <- list("a", "b", "c", "d", "e"); # example list
x[-2]; # without 2nd element
x[-c(2, 3)]; # without 2nd and 3rd
Inoltre, vettori indice logiche sono utili:
x[x != "b"]; # without elements that are "b"
Questo funziona con dataframes anche:
df <- data.frame(number = 1:5, name = letters[1:5])
df[df$name != "b", ]; # rows without "b"
df[df$number %% 2 == 1, ] # rows with odd numbers only
fonte
2009-03-24 21:08:08
Grazie, mylist [i] <- nulla è esattamente il modo di farlo –
Questo non ha funzionato per me.Ottengo: 'Errore nella lista [lunghezza (lista)] <- NULL: la sostituzione ha lunghezza zero' – wbarksdale
@Aleksandr Il post di Levchuck mi ha mostrato che stavo effettivamente trattando un vettore e avevo bisogno di creare un nuovo oggetto – wbarksdale