View source: R/calc_crime_safety.R
calc_crime_safety | R Documentation |
This function computes a crime safety score ranging from 1 to 4 based on survey responses. The score is the mean of two specific item scores, where higher scores indicate a greater sense of crime safety in the neighborhood.
calc_crime_safety(survey_df)
survey_df |
A data frame containing survey data with at least three columns: 'person_id', 'question_concept_id', and 'answer_concept_id'. |
A data frame with two columns: 'person_id' and 'crime_safety', where 'crime_safety' is the calculated crime safety score for each participant. Participants who did not answer both questions will have an NA score.
# Create a sample survey data frame
survey_df <- data.frame(
person_id = c(1, 1, 2, 2, 3, 3, 4, 4),
question_concept_id = c(40192414, 40192492, 40192414, 40192492,
40192414, 40192492, 40192414, 40192492),
answer_concept_id = c(40192514, 40192478, 40192527, 40192422,
40192514, 40192527, 40192422, 40192478)
)
# Compute crime safety scores
crime_safety_scores <- calc_crime_safety(survey_df)
head(crime_safety_scores)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.