Hi All, I have lines in file that look like this:> df[14509227,]SNP A1 A2 freq b se p N 1: <NA> <NA> <NA> NA NA NA NA NA data looks like this:> head(df)SNP A1 A2 freq b se p N 1: rs74337086 G A 0.0024460 0.1627 0.1231 0.1865 218792 2: rs76388980 G A 0.0034150 0.1451 0.1047 0.1660 218792 ...> sapply(df,class)SNP A1 A2 freq b se "character" "character" "character" "numeric" "numeric" "numeric" p N "numeric" "integer"> dim(df)[1] 14509225 8 Tried:> df=na.omit(df) > dim(df)[1] 14509225 8 and:> library(tidyr) > d=df %>% drop_na() > dim(d)[1] 14509225 8 Please advise, Thanks Ana
Hi You should consult either complete.cases function or to remove only rows in which are only NAs you could use something like (untested) df[!(colSums(is.na(df))==8),] Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Ana Marija > Sent: Thursday, September 16, 2021 4:12 PM > To: r-help <r-help at r-project.org> > Subject: [R] unable to remove NAs from a data frame > > Hi All, > > I have lines in file that look like this: > > > df[14509227,] > SNP A1 A2 freq b se p N > 1: <NA> <NA> <NA> NA NA NA NA NA > > data looks like this: > > head(df) > SNP A1 A2 freq b se p N > 1: rs74337086 G A 0.0024460 0.1627 0.1231 0.1865 218792 > 2: rs76388980 G A 0.0034150 0.1451 0.1047 0.1660 218792 ... > > sapply(df,class) > SNP A1 A2 freq b se > "character" "character" "character" "numeric" "numeric" "numeric" > p N > "numeric" "integer" > > > dim(df) > [1] 14509225 8 > > Tried: > > df=na.omit(df) > > dim(df) > [1] 14509225 8 > > and: > > library(tidyr) > > d=df %>% drop_na() > > dim(d) > [1] 14509225 8 > > > Please advise, > > Thanks > Ana > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
You are getting this because you asked for the contents of a row that is beyond the number of rows in your data frame. On Thu, Sep 16, 2021 at 5:12 PM Ana Marija <sokovic.anamarija at gmail.com> wrote:> > Hi All, > > I have lines in file that look like this: > > > df[14509227,] > SNP A1 A2 freq b se p N > 1: <NA> <NA> <NA> NA NA NA NA NA > > data looks like this: > > head(df) > SNP A1 A2 freq b se p N > 1: rs74337086 G A 0.0024460 0.1627 0.1231 0.1865 218792 > 2: rs76388980 G A 0.0034150 0.1451 0.1047 0.1660 218792 > ... > > sapply(df,class) > SNP A1 A2 freq b se > "character" "character" "character" "numeric" "numeric" "numeric" > p N > "numeric" "integer" > > > dim(df) > [1] 14509225 8 > > Tried: > > df=na.omit(df) > > dim(df) > [1] 14509225 8 > > and: > > library(tidyr) > > d=df %>% drop_na() > > dim(d) > [1] 14509225 8 > > > Please advise, > > Thanks > Ana > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hello, You are trying to access elements that do not exist, see the example below: x <- 1:3 x[5] # beyond the last element #[1] NA dim(df) #[1] 14509225 8 df[14509227,] # beyond nrow(df) by 2 Hope this helps, Rui Barradas ?s 15:12 de 16/09/21, Ana Marija escreveu:> Hi All, > > I have lines in file that look like this: > >> df[14509227,] > SNP A1 A2 freq b se p N > 1: <NA> <NA> <NA> NA NA NA NA NA > > data looks like this: >> head(df) > SNP A1 A2 freq b se p N > 1: rs74337086 G A 0.0024460 0.1627 0.1231 0.1865 218792 > 2: rs76388980 G A 0.0034150 0.1451 0.1047 0.1660 218792 > ... >> sapply(df,class) > SNP A1 A2 freq b se > "character" "character" "character" "numeric" "numeric" "numeric" > p N > "numeric" "integer" > >> dim(df) > [1] 14509225 8 > > Tried: >> df=na.omit(df) >> dim(df) > [1] 14509225 8 > > and: >> library(tidyr) >> d=df %>% drop_na() >> dim(d) > [1] 14509225 8 > > > Please advise, > > Thanks > Ana > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >