简体   繁体   中英

Removing certain values from a data frame

I know there are already some threads like this, but I could not find any solutions.

I have a dataframe that looks like this:

    Name        Age     Sex     Survived
1   Allison     0.17    female  1
2   Leah        0.33    female  0
3   David       0.8     male    1
4   Daniel      0.83    male    1
5   Alex        0.83    male    1
6   Jay         0.92    male    1
7   Sara        16      female  1
8   Jade        15      female  1
9   Connor      17      male    1
10  Jon         18      male    1
11  Mary        8       female  1

I want to remove ages that are below 1. I want the data to look like this:

    Name        Age     Sex     Survived
1   Allison     NA      female  1
2   Leah        NA      female  0
3   David       NA      male    1
4   Daniel      NA      male    1
5   Alex        NA      male    1
6   Jay         NA      male    1
7   Sara        16      female  1
8   Jade        15      female  1
9   Connor      17      male    1
10  Jon         18      male    1
11  Mary        8       female  1

Or to just remove the rows with ages < 1 altogether.

Following other solutions I tried this but it didn't work mydata[mydata$Age<"1"&&mydata$Age>"0"] <- NA

Here are three ways to remove the rows:

mydata[mydata$Age > 1, ]

subset(mydata, Age > 1)

filter(mydata, Age > 1)

Here is how to make them NA:

mydata$Age[mydata$Age < 1] <- NA

Your issue is that you are using 1 as a character (in quotes). Character less/greater than work a little differently to numbers so be careful. Also make sure your Age column is numeric. The best way to do that is

mydata$Age <- as.numeric(as.character(mydata$Age))

so you don't accidentally mess up factor variables.

edit put the wrong signs. fixed now

> mydata[mydata$Age<1, "Age"] <- NA
> mydata
      Name Age    Sex Survived
1  Allison  NA female        1
2     Leah  NA female        0
3    David  NA   male        1
4   Daniel  NA   male        1
5     Alex  NA   male        1
6      Jay  NA   male        1
7     Sara  16 female        1
8     Jade  15 female        1
9   Connor  17   male        1
10     Jon  18   male        1
11    Mary   8 female        1

Update

Maybe you can use if Age is factor

mydata[as.numeric(as.character(mydata$Age))<1, "Age"] <- NA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM