Issue with subsetting data from an excel sheet in R

33 Views Asked by At

I'm having issues subsetting data from my excel sheet in r into different segments. The code is as follows:

data<-read_excel("SegmentData.xlsx")

mdata<-data[data$gender=="Male"]

And the error I get is this:

Error in data[data$gender == "Male"] :
✖ Logical subscript `data$gender == "Male"` must be size 1 or 7, not 300.

dput(head(data))result:

structure(list(age = c(36, 38, 45, 42, 45, 44), gender = c("Male", 
"Male", "Female", "Male", "Female", "Male"), income = c(66069, 
63766, 64939, 53163, 72122, 68229), kids = c(2, 3, 1, 4, 1, 4
), ownHome = c("ownNo", "ownNo", "ownNo", "ownNo", "ownYes", 
"ownNo"), subscribe = c("subNo", "subNo", "subNo", "subNo", "subNo", 
"subNo"), Segment = c("Suburb mix", "Suburb mix", "Suburb mix", 
"Suburb mix", "Suburb mix", "Suburb mix")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

There are indeed 300 columns and 7 variables in the excel sheet, but I don't feel like that should be any issue. How do I resolve this? Thanks in advance!

I have tried changing out the variables to others but still run into the same error

1

There are 1 best solutions below

0
B. Christian Kamgang On

You forgot the comma after the logical expression data$gender=="Male"

mdata <- data[data$gender=="Male", ]   # note the command after "Male"

# # A tibble: 4 × 7
#     age gender income  kids ownHome subscribe Segment   
#   <dbl> <chr>   <dbl> <dbl> <chr>   <chr>     <chr>     
# 1    36 Male    66069     2 ownNo   subNo     Suburb mix
# 2    38 Male    63766     3 ownNo   subNo     Suburb mix
# 3    42 Male    53163     4 ownNo   subNo     Suburb mix
# 4    44 Male    68229     4 ownNo   subNo     Suburb mix

data[data$gender=="Male"] will try to subset columns and not rows.