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
You forgot the comma after the logical expression
data$gender=="Male"data[data$gender=="Male"]will try to subset columns and not rows.