R Functions - aggregate(), by(), apply()
Read the data using read.csv
store.df <- read.csv(paste("StoreData.csv", sep=""))
## Warning in file(file, "rt"): cannot open file 'StoreData.csv': No such file
## or directory
## Error in file(file, "rt"): cannot open the connection
Total Sales of Product 2, by Country
aggregate(store.df$p2sales, by=list(country=store.df$country), sum)
## Error in aggregate(store.df$p2sales, by = list(country = store.df$country), : object 'store.df' not found
Average Sales of Product 1 by Store
aggregate(store.df$p1sales, by=list(StoreID = store.df$storeNum), mean)
## Error in aggregate(store.df$p1sales, by = list(StoreID = store.df$storeNum), : object 'store.df' not found
Average Sales of Product 1 by Store
by(store.df$p1sales, store.df$storeNum, mean)
## Error in by(store.df$p1sales, store.df$storeNum, mean): object 'store.df' not found
Average Sales of Product 1 by Store and Year (2001, 2002)
by(store.df$p1sales, list(store.df$storeNum, store.df$Year), mean)
## Error in by(store.df$p1sales, list(store.df$storeNum, store.df$Year), : object 'store.df' not found
Average of store.df columns 2-9
apply(store.df[, 2:9], MARGIN=2, FUN=mean)
## Error in apply(store.df[, 2:9], MARGIN = 2, FUN = mean): object 'store.df' not found
apply(store.df[, 2:9], 2, mean)
## Error in apply(store.df[, 2:9], 2, mean): object 'store.df' not found
Standard Deviation of store.df columns 2-9
apply(store.df[, 2:9], 2, sd)
## Error in apply(store.df[, 2:9], 2, sd): object 'store.df' not found
Applying a User-defined function
apply(store.df[, 2:9], 2, function(x) { mean(x) - median(x) } )
## Error in apply(store.df[, 2:9], 2, function(x) {: object 'store.df' not found