Skip to main content

1.14 Complete Code


1.14 Complete Code


1.      #Setting up working directory
2.      setwd("C:\\SAM\\R\\SA")
3.      getwd()

4.      #Loading data
5.      fin<-read.csv("Future-500.CSV", na.strings = c(""))

6.      #Exploring data
7.      head(fin,20)
8.      tail(fin, 10)
9.      str(fin)
10.  summary(fin)

11.  #Changing from Non-factor to factor
12.  fin$ID <- factor(fin$ID)
13.  summary(fin)
14.  str(fin)
15.  fin$Inception <- factor(fin$Inception)
16.  summary(fin)
17.  str(fin)
18.  fin$Profit <- factor(fin$Profit)
19.  str(fin)

20.  #SUB() and GSUB()
21.  fin$Expenses <- gsub(" Dollars", "", fin$Expenses)
22.  head(fin)
23.  fin$Expenses <- gsub(",", "", fin$Expenses)
24.  head(fin)
25.  fin$Revenue <- gsub("\\$", "", fin$Revenue)
26.  head(fin)
27.  fin$Revenue <- gsub(",", "", fin$Revenue)
28.  head(fin)
29.  str(fin)
30.  fin$Growth <- gsub("\\%", "", fin$Growth)
31.  str(fin)
32.  fin$Expenses <- as.numeric(fin$Expenses)
33.  fin$Revenue <- as.numeric(fin$Revenue)
34.  fin$Growth <- as.numeric(fin$Growth)
35.  fin$Profit <- as.numeric(as.character((fin$Profit)))

36.  head(fin,24)
37.  complete.cases(fin)
38.  fin[!complete.cases(fin),]

39.  #filtering using which() for non-missing data
40.  head(fin)
41.  fin[fin$Revenue==8567910,]
42.  which(fin$Revenue==8567910)
43.  fin[which(fin$Revenue==8567910),]
44.  fin[fin$Employees==45,]
45.  fin[which(fin$Employees==45),]
46.  head(fin,24)
47.  fin[fin$Expenses==NA,]

48.  a<-c(1,24,53,NA, 68, NA)
49.  is.na(a)
50.  fin[is.na(fin$Expenses),]

51.  #removing data with missing data
52.  fin_backup <- fin
53.  fin[!complete.cases(fin),]
54.  fin[is.na(fin$Industry),]
55.  fin <- fin[!is.na(fin$Industry),]
56.  fin
57.  fin[!complete.cases(fin),]

58.  #resetting Dataframe index
59.  rownames(fin) <- 1:nrow(fin)
60.  fin
61.  rownames(fin) <- NULL
62.  fin

63.  #Replacing missing data with factual analysis
64.  fin[!complete.cases(fin),]
65.  fin[is.na(fin$State),]
66.  fin[is.na(fin$State) & fin$City == "New York",]
67.  fin[is.na(fin$State) & fin$City == "New York","State"] <- "NY" 

68.  #Checking it with the row number
69.  fin[c(11,377),]
70.  fin[!complete.cases(fin),]
71.  fin[is.na(fin$State) & fin$City == "San Francisco",]
72.  fin[is.na(fin$State) & fin$City == "San Francisco","State"] <- "CA"

73.  #Checking it with the row number
74.  fin[c(82,265),]
75.  fin[!complete.cases(fin),]

76.  #Replacing missing data: Median Imputation method
77.  fin[!complete.cases(fin),]
78.  median(fin[,"Employees"])
79.  median(fin[,"Employees"], na.rm = TRUE)
80.  med_empl_retail <- median(fin[fin$Industry=="Retail","Employees"], na.rm = TRUE)
81.  med_empl_retail
82.  fin[is.na(fin$Employees) & fin$Industry == "Retail", "Employees"] <- med_empl_retail
83.  #check
84.  fin[3,]

85.  #Dealing with missing data in Financial Services
86.  med_empl_fs<-median(fin[fin$Industry=="Financial Services","Employees"],na.rm=TRUE)
87.  med_empl_fs
88.  fin[is.na(fin$Employees) & fin$Industry == "Financial Services", "Employees"]<- med_empl_fs
89.  #check
90.  fin[330,]

91.  #Dealing with missing data in Growth
92.  med_growth_construction <- median(fin[fin$Industry=="Construction","Growth"], na.rm = TRUE)
93.  med_growth_construction
94.  fin[is.na(fin$Growth) & fin$Industry == "Construction", "Growth"] <- med_growth_construction
95.  #check
96.  fin[8,]

97.  #Revenue
98.  med_rev_constr <- median(fin[fin$Industry=="Construction","Revenue"], na.rm = TRUE)
99.  med_rev_constr
100.                      fin[is.na(fin$Revenue) & fin$Industry == "Construction", "Revenue"] <- med_rev_constr
101.                      fin[!complete.cases(fin),]

102.                      #Expenses
103.                      #Be careful here, because we are performing operation only for certain ones
104.                      med_exp_constr <- median(fin[fin$Industry=="Construction","Expenses"], na.rm = TRUE)
105.                      med_exp_constr
106.                      fin[is.na(fin$Expenses) & fin$Industry == "Construction" & is.na(fin$Profit), "Expenses"] <- med_exp_constr
107.                      fin[!complete.cases(fin),]

108.                      #Replacing missing data : Deriving values
109.                      #Revenue = Expenses – Profit
110.                      #Expenses = Revenue – Profit
111.                      fin[is.na(fin$Profit),"Profit"] <- fin[is.na(fin$Profit),"Revenue"] - fin[is.na(fin$Profit),"Expenses"]
112.                      #check
113.                      fin[c(8,42),]

114.                      fin[!complete.cases(fin),]
115.                      fin[is.na(fin$Expenses),"Expenses"] <- fin[is.na(fin$Expenses),"Revenue"] - fin[is.na(fin$Expenses),"Profit"]
116.                      #check
117.                      fin[15,]
118.                      fin[!complete.cases(fin),]

Comments

Popular posts from this blog

1.9 Removing Records with Missing Data

1.9 Removing Records with Missing Data In the post number 1.5 Dealing with missing data, we saw various methods and lets implement few of those in this tutorial. First of all let's have a look at the CSV file. We see that, we have decided the option of removing rows where values in Industry column are missing. Before proceeding to the R, I would suggest you to always make a back up of the data so that in case you do any mistake in between you always have the original data to start again. Let's create the backup of our fin dataset. . And this one line can save us a lot of trouble. Now, let's find out all of the rows that have empty value in any of the column. We see two rows where values in Industry column is missing. Let's single out these rows using is.na() So we got two rows with ID 14, 15 where value in Industry column is missing. Now to remove these two rows, we just do the opposite and find out the rows which don't have NA in them and assign it b...

1.3 The Factor Variable Trap

1.3 The Factor Variable Trap The Factor Variable Trap or the FVT comes into play when we ate trying to convert a variable from factor to non-factor. It is a known phenomenon, but isn’t very well publicized. Let’s create a vector named a with the values “12“,“13“,“14“,“12”, “12”. (five values all in quotation marks. The values are in character because of double quotation marks, we can verify this with the function typeof() Now let’s convert this vector into type integer with the function an.numeric() So, the above code was to convert characters into numeric. but how to convert factors into numeric?? For this, let’s create a factor Z which contains exactly same values as of vector a.   When we run the above command, output is shown without quotation marks and levels are also displayed. Thus R is recognizing it as categories. Now let’s convert it into numeric, as done before and save it in vector Y to see the output. OOPS!!! What happ...
1.8 Data Filters: is.na() for Missing Data In previous post we have learned how to filter data for non missing data. In this one, we will learn how to filter out missing data using is.na(). Let's look at first 24 rows using head() to see the missing values. Just like previous post, if we use the same logic we get NA. Thus, it is not helping at all. The other way to tackle this is is.na(). This function checks if the value contained is NA or not. We try this function, by creating a vector named "a" putting some NAs in it and checking it with is.na(). It gives the value FALSE if its not NA and TRUE if it contains NA. We will use the similar function for our dataset to find out NA in Revenue column. It correctly identifies the values in Revenue column which are equal to NAs. Try to implement it in other columns as well and find out the rows which contains missing values .