-1

I am new to R and learning how to code. Right now I have a dataframe from dslab called heights and it looks like this:

    Sex    Height
1   Male     75
2   Male     70
3   Male     68
4   Male     74
5   Male     61
6   Female   65

However, now I want to filter out the male observations and put the female observations into a new dataframe. How do I go about this?

Joe
  • 11
  • 2

1 Answers1

0

You can use == to get a logical vector which can be used for subsetting using [.

i <- x$Sex == "Male"

(male <- x[i,])
#   Sex Height
#1 Male     75
#2 Male     70
#3 Male     68
#4 Male     74
#5 Male     61

(female <- x[!i,])
#     Sex Height
#6 Female     65

Data:

x  <- read.table(header=TRUE, text="    Sex    Height
1   Male     75
2   Male     70
3   Male     68
4   Male     74
5   Male     61
6   Female   65")
GKi
  • 37,245
  • 2
  • 26
  • 48