Come posso cancellare ogni n-esima riga da un dataframe in R?Eliminazione di ogni n-esima fila in un dataframe
risposta
Si potrebbe creare una funzione come segue
Nth.delete<-function(dataframe, n)dataframe[-(seq(n,to=nrow(dataframe),by=n)),]
Testiamola fuori
DF<-data.frame(A=1:15, B=rnorm(15), C=sample(LETTERS,15))
Nth.delete(DF, 3)
quindi df [ -rowNumber,] cancella quella particolare riga – MySchizoBuddy
questa è corretta –
Se si desidera ottenere il ciascuna delle colonne ennesimi da un frame di dati o un vettore ecc uso modulo subsetting ...
Selezionare le colonne nth ripetendo set qui come modulo di 3 (scegliere nth come si desidera)
> x <- c(1,2,3,4,5,6)
> d <- rbind(x,x,x)
> df <- as.data.frame(d, row.names=T)
> c <- 1:ncol(df)
> c
[1] 1 2 3 4 5 6
c%%3 ### nth cycle, here every 3
[1] 1 2 0 1 2 0
#select the every 3rd column of every 3
> df[, c%%3==0]
V3 V6
1 3 6
2 3 6
3 3 6
#every first column of every 3
> df[, c%%3==1]
V1 V4
1 1 4
2 1 4
3 1 4
#every 2nd column of every 3
> df[, c%%3==2]
V2 V5
1 2 5
2 2 5
3 2 5
#drop the 3rd columns
> df[, !(c%%3==0)]
V1 V2 V4 V5
1 1 2 4 5
2 1 2 4 5
3 1 2 4 5
ecc ... di swap c < -nrow (df) per le righe subsetting ..
Ecco un modo per farlo. 'df [seq (1, NROW (df), di = n),]' – Ramnath
@Ramnath: 'df [-seq (n, NROW (df), di = n),]' potrebbe funzionare meglio – Henry