0% found this document useful (0 votes)
45 views

Data Preprocessing Assignment Solutions: Error in ' (.Data - Frame' (Mtcars, 1:20) ) : Undefined Columns Selected

1. The document provides solutions to data preprocessing exercises involving the mtcars dataset. 2. It demonstrates how to subset the mtcars data frame to extract rows that meet certain criteria, such as having 4 cylinders or weighing less than the mean. 3. Transformations are also shown, like converting a date column to a proper date format and renaming a variable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Data Preprocessing Assignment Solutions: Error in ' (.Data - Frame' (Mtcars, 1:20) ) : Undefined Columns Selected

1. The document provides solutions to data preprocessing exercises involving the mtcars dataset. 2. It demonstrates how to subset the mtcars data frame to extract rows that meet certain criteria, such as having 4 cylinders or weighing less than the mean. 3. Transformations are also shown, like converting a date column to a proper date format and renaming a variable.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data preprocessing assignment

Solutions
1. The command mtcars[1:20] returns an error because there is no column defined hence we are
getting error.
The resultant output shown was - Error in `[.data.frame`(mtcars, 1:20)] : undefined columns
selected

It differs from the similar command mtcars[1:20, ] as in this new command, the column is
defined. The “ “ defines column in the command and as column is specified it will not show any
error.

2. i. mtcars[mtcars$cyl = 4, ]

answer - mtcars[mtcars$cyl == 4, ]

Screenshot of output attached. 11 observations of 11 variables are found.

ii. mtcars[-1:4, ]

answer - mtcars[1:4, ]

Screenshot of output attached. 4 observations of 11 variables are found.

iii. mtcars[mtcars$cyl <= 5]


answer- mtcars[mtcars$cyl <= 5, ]

Screenshot of output attached. 11 observations of 11 variables are found.

iv. mtcars[mtcars$cyl == 4 | 6, ]

answer - mtcars[mtcars$cyl == 4 | mtcars$cyl == 6, ]

Screenshot of output attached. 18 observations of 11 variables are found.


3. in order to rename from “merc” to “Mercedes”, the following codes are required
Q3<-rownames(mtcars)
Q3 = gsub("Merc","Mercedes",Q3)
rownames(mtcars)<-Q3

Screenshot of output attached.

4. Records with cylinder greater than 4 and weighs less than mean weight are -

mean(mtcars$wt)
Q4<- subset(mtcars,cyl > 4 & mtcars,wt < 3.21725, )

Screenshot of output attached. 5 observations of 11 variables are found.


5. Required tasks are –
- Conversion of the Permit Creation Date column in the original dataset from character to
proper date format.
- Extraction of the building records with permit date before 1 January 2013.
- Finding the oldest and newest permit date records for the buildings

Codes for the above are

BP<- Building_Permits
BP$`Permit Creation Date`<-as.Date(BP$`Permit Creation Date`, format = "%m/%d/%Y")
q1<-BP[BP$`Permit Creation Date` < "2013-1-1", ]
q2<-BP[order(BP$`Permit Creation Date`), ]
q3<-tail(q2,1)
q4<-head(q2,1)

Screenshot of output attached. 22 observations of 43 variables are found.

NEW
OLD

6.
i) Extraction of the building records with permit date after 1 January 2015 -
q5<-BP[BP$`Permit Creation Date` > "2015-1-1", ]

Screenshot of output attached. 126964 observations of 43 variables found.

ii) Finding the oldest and newest permit date records for the buildings in Block 326 -

BP$Block<-as.numeric(BP$Block)
q6<-BP[order(BP$`Permit Creation Date`), ]
q7<-subset(q6,q6$Block =="326", )
q8<-tail(q7,1)
q9<-head(q7,1)
NEW

OLD

iii) extracting the building records with permit date after 1 January 2015 and completed
after 1 January 2018 -

BP$`Completed Date`<-as.Date(BP$`Completed Date`, format = "%m/%d/%Y")


Q6<-subset(BP,BP$`Completed Date` >"2018-1-1" & BP$`Permit Creation Date` > "2015-1-1", )

Screenshot of output attached. 3130 of 43 variables are found.

You might also like