0

I have a variable "bwt". I am needing to create 4 frequency histograms of the bwt by the race of the individuals. There are 4 races under the name "race": white, black, native american, and other. I know how to make histograms with the hist function, but how would would I separate bwt out for each of the 4 races so I can produce these histograms? I've included the data below.

##   patnum hospstay    lowph pltct  race  bwt gest        inout twn lol magsulf
## 1      1       34       NA   100 white 1250   35 born at duke   0  NA      NA
## 2      2        9 7.250000   244 white 1370   32 born at duke   0  NA      NA
## 3      3       -2 7.059998   114 black  620   23 born at duke   0  NA      NA
## 4      4       40 7.250000   182 black 1480   32 born at duke   0  NA      NA
## 5      5        2 6.969997    54 black  925   28 born at duke   0  NA      NA
## 6      6       62 7.189999    NA white  940   28 born at duke   0  NA      NA
##   meth toc  delivery apg1 vent pneumo pda cld    sex dead
## 1    0   0 abdominal    8    0      0   0   0 female    0
## 2    1   0 abdominal    7    0      0   0   0 female    0
## 3    0   1   vaginal    1    1      0   0  NA female    1
## 4    1   0   vaginal    8    0      0   0   0   male    0
## 5    0   0 abdominal    5    1      1   0   0 female    1
## 6    1   0 abdominal    8    1      0   0   0 female    0

The code I used to make a simple histogram is included below.

hist(SB_xlsx$bwt, ylab="frequency", xlab="Birth Weight", main="Histogram of Birth Weight")

I do not know how to go about getting the bwt separated by race so I can produce the 4 separate histograms of bwt by race.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
barnsm2
  • 185
  • 7
  • Please make this a reproducible issue by sharing some of your data and the code you've tried so far. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on how to post a good question. – Dan Adams Feb 17 '22 at 18:52

1 Answers1

1

Please upload a dataset for us to work with next time.

The answer you are looking for is quite simple, it's in the package library(dplyr)using the command filter()

Since I don't have your dataset, I made one up, with four races, under the column "bwt", and the other column, I called "height", this is what you will make a histogram with

# Create an empty data frame with column names
example_df <- data.frame( "bwt" = character(0), "height" = integer(0))
#Assign names to x 
variable_names <- c( "Black", "White", "Native-American", "Other")
# Assign names to y
w<-rnorm(200, mean=5, sd=2)
x<-rnorm(200, mean=5, sd=2)
y<-rnorm(200, mean=5, sd=2)
z<-rnorm(200, mean=5, sd=2)
#combine everything to create dataframe (df)
df <- data.frame( "btw" = variable_names, "height" = c(w,x,y,z))
attach(df)
#load necessary package
library(dplyr)
# Use the filter() command to select the races you want
black = filter(df,btw=="Black")
white = filter(df,btw=="White")
native = filter(df,btw=="Native-American")
other = filter(df,btw=="Other")
# make histograms (I will only upload one image as an example here)
hist(black$height)
hist(white$height)
hist(native$height)
hist(other$height)

[![enter image description here][1]][1]

# you can also plot all 4 in one window

par(mfrow = c(2, 2), cex = 1)

hist(black$height,col="blue")
hist(white$height, col="green")
hist(native$height, col="orange")
hist(other$height, col="grey23")

'''

[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/OuWkx.png
  [2]: https://i.stack.imgur.com/t5rDp.png
Andy
  • 413
  • 2
  • 15