bw = read.csv("/Users/tramanhvo/Downloads/bw_file.csv", header=T, na.strings = "")

Assign factor (discontinuous data values) to “race” variable numbers & create a new column to record those values.

bw$ethnicity [bw$race==1] <- "White"
bw$ethnicity [bw$race==2] <- "Black"
bw$ethnicity [bw$race==3] <- "Other"

Add new column that replaces binary numbers for “Smoking” variable with “Yes”/“No”.

bw$smoking = ifelse(bw$smoke==1, "Yes", "No")

Add new column that replaces binary numbers for “low.bwt” variable with “Yes”/“No”.

bw$low.bwt = ifelse(bw$low==1, "Yes", "No")

Create table using the table1 function, with “ethincity” as colunns and the variables in front as rows. The talbe will also show stdev, mean, and other statistical values.

echo=FALSE prevents code that creates the chart from being shown.

Black
(N=26)
Other
(N=67)
White
(N=96)
Overall
(N=189)
age
Mean (SD) 21.5 (5.11) 22.4 (4.54) 24.3 (5.65) 23.2 (5.30)
Median [Min, Max] 20.5 [15.0, 35.0] 22.0 [14.0, 33.0] 23.5 [14.0, 45.0] 23.0 [14.0, 45.0]
smoking
No 16 (61.5%) 55 (82.1%) 44 (45.8%) 115 (60.8%)
Yes 10 (38.5%) 12 (17.9%) 52 (54.2%) 74 (39.2%)
low.bwt
No 15 (57.7%) 42 (62.7%) 73 (76.0%) 130 (68.8%)
Yes 11 (42.3%) 25 (37.3%) 23 (24.0%) 59 (31.2%)
bwt
Mean (SD) 2720 (639) 2810 (722) 3100 (728) 2940 (729)
Median [Min, Max] 2850 [1140, 3860] 2840 [709, 4050] 3060 [1020, 4990] 2980 [709, 4990]