[rsplus] 1)

Viewer

  1.  
  2. datamain_set <- read.csv('CTNS2021ectn_p.csv')
  3. install.packages("magrittr") # package installations are only needed the first time you use it
  4. install.packages("dplyr")    # alternative installation of the %>%
  5. install.packages("tidyverse")
  6. library(magrittr) # needs to be run every time you start R and want to use %>%
  7. library(dplyr) 
  8. library(tidyverse)
  9. datamain_set
  10.  
  11. #TBC_05BR Age First time smoked whole cigarette : How old were you when you first smoked whole cigarette (= 3 or 96)
  12. #VAP_05BR Tried vaping liftime: How old were you when you first tied vaping (<=2)
  13. #VAP_60 Perception harm to persons health (Vaping vs Cigarretes)
  14.  
  15. datamain <- datamain_set %>%
  16.   select(PUMFID,TBC_40R,
  17.          TBC_05BR,
  18.          VAP_05BR,
  19.          VAP_60,
  20.          AGEGROUP,GENDER)
  21. datamain <- datamain %>%
  22.   filter(GENDER== c(1,2))
  23.  
  24. datamain$GENDER <-factor(datamain$GENDER, 
  25.                          levels = c(1,2)
  26.                          labels = c("Male","Female"))
  27.  
  28.  
  29. #-----Ciggarettes---------
  30.  
  31.   tab_c1 <- datamain %>%
  32.     filter(AGEGROUP ==3,
  33.            TBC_05BR == c(03,96)) #= 0 - 3; 0 - 19 year old, 96 - Valid Skip. 
  34.   tab_c1 <- select(tab_c1,AGEGROUP,TBC_05BR,GENDER)
  35.  
  36.   tab_c1<- tab_c1 %>%
  37.     mutate(smoke = ifelse(TBC_05BR == 31,0))
  38.   
  39.   tab_c1$AGEGROUP <-factor(tab_c1$AGEGROUP,
  40.                           levels = c(3),
  41.                           labels = c("15 to 19 years old"))
  42.   
  43.   tab_c1$TBC_05BR <-factor(tab_c1$TBC_05BR,
  44.                            levels = c(3,96),
  45.                            labels = c("Yes","No"))
  46.  
  47.  #Data filling for tables
  48.             xc <- tibble(filter(tab_c1, GENDER == "Female"))   #smoke - Male
  49.             xc <- xc %>%
  50.               select(GENDER,smoke)
  51.             round(sd(xc$smoke),2)
  52.             round(mean(xc$smoke),2)
  53.            nrow(xc)
  54.             yc <- tibble(filter(tab_c1, GENDER == "Male"))   #smoke - Male
  55.             yc <- yc %>%
  56.               select(GENDER,smoke)
  57.             
  58.             round(sd(yc$smoke),2)   #sd
  59.             round(mean(yc$smoke),2)   #mean
  60.            nrow(yc)
  61.            
  62.            round(sd(tab_c1$smoke),2)
  63.            round(mean(tab_c1$smoke),2)
  64.  
  65.   #Final Data for Smoking 
  66.   tabc1 <- as.data.frame.matrix(table(tab_c1$AGEGROUP,tab_c1$TBC_05BR)) ## Frequency of 15 to 19 year olds Vs Tried Smoking  | Convert to CSV file for easy copy pasting
  67.   tabc1p <- as.data.frame.matrix(prop.table(table(tab_c1$AGEGROUP,tab_c1$TBC_05BR), margin=1)) ## Proportion of 15 to 19 year olds VS Tried smoking | Convert to CSV file for easy copy pasting
  68.  
  69.   tabc1g <- as.data.frame.matrix(table(tab_c1$GENDER,tab_c1$TBC_05BR)) ## Frequency of Sexes ages 15 to 19 year olds VS Tried smoking | Convert to CSV file for easy copy pasting
  70.   
  71.   tabc1pg <- as.data.frame.matrix(prop.table(table(tab_c1$GENDER,tab_c1$TBC_05BR), margin=1)) ## Propotion of Sexes ages 15 to 19 year olds VS Tried smoking| Convert to CSV file for easy copy pasting
  72.  
  73.   chisq.test(tab_c1$GENDER,tab_c1$smoke)$expected 
  74.   t<-as.data.frame.matrix(chisq.test(tab_c1$GENDER,tab_c1$smoke)$expected,1) #  | Convert to CSV file for easy copy pasting
  75.   chisq.test(tab_c1$smoke,tab_c1$GENDER)
  76.   
  77.   
  78. #------Vaping-------
  79.   tab_v1 <- datamain %>%
  80.     filter(AGEGROUP==03,VAP_05BR == c(03,96)) #= 0 - 3; 0 - 19 year old, 96 - Valid Skip. 
  81.   tab_v1 <- select(tab_v1,AGEGROUP,VAP_05BR,GENDER)
  82.   
  83.   tab_v1<- tab_v1 %>%
  84.     mutate(vape = ifelse(VAP_05BR == 03, 1,0))
  85.   
  86.  
  87.   
  88.   
  89.   
  90.   tab_v1$AGEGROUP <-factor(tab_v1$AGEGROUP,
  91.                            levels = c(3),
  92.                            labels = c("15 to 19 years old"))
  93.   
  94.   tab_v1$VAP_05BR <-factor(tab_v1$VAP_05BR,
  95.                            levels = c(3,96),
  96.                            labels = c("Yes","No"))
  97.   
  98.   # Data filling for Tables
  99.             x <- tibble(filter(tab_v1, GENDER == "Female"))   #vape - Fema
  100.             x <- x %>%
  101.               select(GENDER,vape)
  102.             round(sd(x$vape),2)
  103.             round(mean(x$vape),2)
  104.             
  105.             y <- tibble(filter(tab_v1, GENDER == "Male"))   #vape - Male
  106.             y <- y %>%
  107.               select(GENDER,vape)
  108.             round(sd(y$vape),2)   #sd
  109.             round(mean(y$vape),2)   #mean
  110.             
  111.             mean(tab_v1$vape)
  112.   
  113.   #Final Data for Vaping
  114.   tabv1 <- as.data.frame.matrix(table(tab_v1$AGEGROUP,tab_v1$VAP_05BR)) ## Frequency of 15 to 19 year olds Vs Tried Vaping  |Convert to CSV file for easy copy pasting
  115.   tabv1p <- as.data.frame.matrix(prop.table(table(tab_v1$AGEGROUP,tab_v1$VAP_05BR), margin=1)) ## Frequency of 15 to 19 year olds Vs Tried Vaping | Convert to CSV file for easy copy pasting
  116.   
  117.   tabv1g <- as.data.frame.matrix(table(tab_v1$GENDER,tab_v1$VAP_05BR)) ## Frequency of Sexes ages 15 to 19 year olds VS Tried Vaping | Convert to CSV file for easy copy pasting
  118.   tabv1pg <- as.data.frame.matrix(prop.table(table(tab_v1$GENDER,tab_v1$VAP_05BR), margin=1)) ## Proportion of Sexes ages 15 to 19 year olds VS Tried Vaping| Convert to CSV file for easy copy pasting
  119.   
  120.  
  121.   t<-as.data.frame.matrix(chisq.test(tab_v1$GENDER,tab_v1$vape)$expected,1) ## Convert to CSV file for easy copy pasting
  122.   chisq.test(tab_v1$vape,tab_v1$GENDER) #CHI SQUARE TEST
  123.  

Editor

You can edit this paste and save as new:


File Description
  • 1)
  • Paste Code
  • 04 Dec-2022
  • 5.13 Kb
You can Share it: