simple doubts in R

Hi,
I am the beginner in R-Studio a1n not able to solve these queries. Plz help me out.

  1. Use the AdultUCI data set from the arules() package. How many 38 year olds are divorced?
    What is wrong with this code??

install.packages("arules")
library(arules)
data("AdultUCI")
str(AdultUCI)
install.packages("dplyr")
library("dplyr")
fd3<-filter(AdultUCI, age=38 & marital-status="Divorced")

2)Use the lackers data set in the package lubridate. In this data how many rows correspond to instances where the day was Monday and time 12?
I have tried this code but not getting output.

install.packages("lubridate")
install.packages("dplyr")
library(lubridate)
data("lakers")
str(lakers)
library("dplyr")
data("lakers")
str(lakers)
dim(fd)
fd<-filter(lakers, day==1 & time=="12:00")
ds2<-filter(lakers, wday(date)=="Monday" & time=="12:00")
Thanks

Hi @priks_24,

Welcome to the community!

Your question seem to pertain to homework, so please read the Homework Policy.

You won't learn anything from being giving the answers, so please do show us what you have tried and then I'm sure people will be happy no nudge you in the right direction :slightly_smiling_face:

2 Likes

You have some syntax errors
The "exactly equal to" operator is == not =
The "AND" operator (&) is no needed on dplyr syntax
marital-status is a non-syntactic variable name so it has to be referenced among backticks like this `marital-status`

library(dplyr)
library(arules)

data("AdultUCI")

filter(AdultUCI, age == 38, `marital-status` == "Divorced") 
#>   age workclass fnlwgt    education education-num marital-status
#> 1  38   Private 215646      HS-grad             9       Divorced
#> 2  38   Private 155222 Some-college            10       Divorced
#> 3  38   Private 179488 Some-college            10       Divorced
#> 4  38   Private 169469      HS-grad             9       Divorced
#> 5  38   Private 411797    Assoc-voc            11       Divorced
#> 6  38   Private 278924 Some-college            10       Divorced
#>          occupation  relationship  race    sex capital-gain capital-loss
#> 1 Handlers-cleaners Not-in-family White   Male            0            0
#> 2 Machine-op-inspct Not-in-family Black Female            0            0
#> 3      Craft-repair Not-in-family White   Male            0         1741
#> 4             Sales Not-in-family White   Male            0            0
#> 5      Adm-clerical     Unmarried White Female            0            0
#> 6      Craft-repair Not-in-family White   Male            0            0
#>   hours-per-week native-country income
#> 1             40  United-States  small
#> 2             28  United-States  small
#> 3             40  United-States  small
#> 4             80  United-States  small
#> 5             40  United-States  small
#> 6             44  United-States  small

Created on 2020-06-17 by the reprex package (v0.3.0)

Thanks for your guidance.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.