Facebook
From Bistre Matamata, 3 Years ago, written in R.
Embed
Download Paste or View Raw
Hits: 47
  1. #Question 1
  2. # a.    Write a SAS program to create a temporary SAS data set called Vote.
  3. # Use the variable name State, Party and Age. Age should be stored as a numeric variable,
  4. # State and Party should be stored as character variables.
  5. # b.    Include a procedure to list the observations in this data set.
  6. # c.    Include a procedure to compute frequencies for Party.
  7. # d.    Repeat problem above (a to c) using R programming
  8. library(readr) #to read csv file
  9. setwd("D:/Rstudio Project")
  10. Vote2 <- read.csv(file = 'D:/Rstudio Project/Sas Week 12/political.csv', header =FALSE , sep=',')
  11. colnames(Vote2) <- c("State" , "Party", "Age") #change column names
  12. View(Vote2) #list all observation
  13. print(Vote2) #list all observation
  14.  
  15. #calculate frequency
  16. #Method 1
  17. table(Vote2$Party)
  18. #Method 2
  19. p <- as.data.frame(table(Vote2$Party))
  20. names(p)[1] = "Party"
  21. View(p)
  22.  
  23. summary(p$Party) #View summary
  24.  
  25.  
  26.  
  27. # Question 2
  28. # Using the Bicycle data set (SAS native dataset), list all the
  29. # observations for Road Bike that cost more than $2,500 or Hybrid
  30. # that cost more than $660. The variable Model contains the type of bike
  31. # and UnitCost contains the cost.
  32.  
  33. #teachers way, using sa7bdat library
  34. library(sas7bdat)
  35. bicycles2 <- read.sas7bdat(file = 'D:/Rstudio Project/Sas Week 12/bicycles.sas7bdat')
  36. outpl <- subset(bicycles2, (Model=='Road Bike' & UnitCost > 2500 | Model=='Hybrid' & UnitCost > 660), select=Country:Manuf)
  37. print(outpl)
  38.  
  39. #own way
  40. bicycles <- read_sas("bicycles.sas7bdat", + NULL)
  41. View(bicycles)
  42. library(sqldf)
  43. sqldf("select * from bicycles where Model = 'Road Bike' and UnitCost > 2500 or Model = 'Hybrid' and Unitcost >660") #get the result using sql
  44.  
  45. RoadBike <- bicycles[bicycles$UnitCost > 2500 & bicycles$Model == "Road Bike" ,] #get the result for roadbike
  46. Hybrid <- bicycles[bicycles$UnitCost > 660 & bicycles$Model == "Hybrid" ,] #get the result for roadbike
  47. Mergedbike <- merge(Hybrid,RoadBike, all = TRUE) #merge both datsets together
  48. View(Mergedbike)
  49.  
  50. #Question 3
  51. # Create a temporary SAS data set called Bank using this data file. Use using formatted
  52. # input to read the data values. Include in this data set a variable called Interest
  53. # computed by multiplying Balance by Rate. List the contents of this data set using
  54. # PROC PRINT.
  55. library(readr)
  56. library(tidyr)
  57. bankdata <- read_table2("bankdata.txt", col_names = FALSE)                      
  58. View(bankdata)
  59. bankdata <- unite(bankdata, Name, X1, X2, sep = " ")
  60. bankdata$Rate <- 0
  61. colnames(bankdata) <- c("Name" , "Acct", "Balance", "Rate")
  62. View(bankdata)
  63. bankdata$Interest <- bankdata$Balance * bankdata$Rate
  64. View(bankdata)
  65.  
  66. #teachers solution
  67. bankR <- read.fwf('D:/Rstudio Project/Sas Week 12/bankdata.txt', width=c(15,5,6,4), col.names = c("Name" , "Acct", "Balance", "Rate"))
  68. bankR$interest = bankR$balance = bankR$rate
  69. print(bankR)
  70.