Reputation: 227
I want to code a function which has a state abbreviation called state
as its only input variable.
The function should then ..
read.csv()
split()
state
I know it is kind of a filter function, but I explicitly want to do it with the split
function.
Here is my code:
best <- function(state) {
## Read the data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## Split data up by the 'State' column
data_split <- split(data, data$State)
## Return data with state in 'State' column
data_split$state
}
## Executing the function
best("NY") ## returns NULL
When I execute these lines without using a function, it is working. So I suppose that there is a problem with putting the input variable state
as an argument in data_split$state
.
Thanks for your help.
Best wishes, Marcus
Upvotes: 0
Views: 193
Reputation: 521249
Can you try using:
data_split[[state]]
Full code:
best <- function(state) {
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
# split data on the _column_ called 'State'
data_split <- split(data, data$State)
# return the entry in the list whose _name_ is contained in 'state'
data_split[[state]]
}
Upvotes: 1