Reputation: 3
For some reason no matter what I go about trying too append values to a list of mine. I cannot seem to get it right. What I have tried:
suburb_shootings <- list()
add_shootings_to_suburb_list <- function(){
total_rows <- nrow(shooting_cases[4])
for(x in 1:total_rows){
suburb_shootings[[x]] <- shooting_cases[x,4]
}
}
add_shootings_to_suburb_list()
Alternatively:
add_shootings_to_suburb_list <- function(){
total_rows <- nrow(shooting_cases[4])
for(x in 1:total_rows){
suburb_shootings[[x]] <- append(suburb_shootings, shooting_cases[x,4])
}
}
add_shootings_to_suburb_list()
OR:
add_shootings_to_suburb_list <- function(){
suburb_shootings <- list()
total_rows <- nrow(shooting_cases[4])
for(x in 1:total_rows){
suburb_shootings <- append(suburb_shootings, shooting_cases[x,4])
}
}
add_shootings_to_suburb_list()
This is to be used for visualisation charts later on but I essentially just need to create a list of all suburbs where shooting incidents that occurred in NYC during a time period took place. Even though there may be duplicate suburbs. I.E: "Brooklyn" may repeat itself x amount of times as more than one shooting incident may have occurred on separate occasions
I am new to R, so it is possible I am not using the list data type correctly.
Please correct me with what I am doing wrong.
A line snippet from the relevant CSV file is as follows:
INCIDENT_KEY,OCCUR_DATE,OCCUR_TIME,BORO,PRECINCT,JURISDICTION_CODE,LOCATION_DESC,STATISTICAL_MURDER_FLAG,PERP_AGE_GROUP,PERP_SEX,PERP_RACE,VIC_AGE_GROUP,VIC_SEX,VIC_RACE,X_COORD_CD,Y_COORD_CD,Latitude,Longitude,Lon_Lat
236168668,11/11/2021,15:04:00,BROOKLYN,79,0,,false,,,,18-24,M,BLACK,996313,187499,40.68131820000008,-73.95650899099996,POINT (-73.95650899099996 40.68131820000008)
231008085,07/16/2021,22:05:00,BROOKLYN,72,0,,false,45-64,M,ASIAN / PACIFIC ISLANDER,25-44,M,ASIAN / PACIFIC ISLANDER,981845,171118,40.63636384100005,-74.00866668999998,POINT (-74.00866668999998 40.63636384100005)
230717903,07/11/2021,01:09:00,BROOKLYN,79,0,,false,<18,M,BLACK,25-44,M,BLACK,996546,187436,40.68114495900005,-73.95566903799994,POINT (-73.95566903799994 40.68114495900005)
The data set is some 20k + lines long.
Below is a screenshot of how it is read in as a csv
Upvotes: 0
Views: 67
Reputation: 146224
Functions don't modify objects outside of their scope. (Unless you use global assignment, which you really shouldn't...)
Your last attempt is almost correct, you just need the function to return the result, and then assign it when you call the function. Functions return whatever the last line of the function is.
add_shootings_to_suburb_list <- function(){
suburb_shootings <- list()
total_rows <- nrow(shooting_cases[4])
for(x in 1:total_rows){
suburb_shootings <- append(suburb_shootings, shooting_cases[x,4])
}
suburb_shootings
}
my_list <- add_shootings_to_suburb_list()
my_list # should print your result
That said, you don't need a function for this. Your function looks like an inefficient way to write suburb_shootings <- as.list(shooting_cases[[4]])
. Without seeing your data or knowing your goal, I'm also suspicious that you need a list
here at all... is there a reason you don't keep it as a vector?
Upvotes: 1