Reputation: 559
I have searched SO and found a similar question, but I still can't get it to work.
I am rather new to R programming, so I appreciate this help.
Suppose I have a data.frame
:
df <- data.frame(State = c("NJ", "MA", "CA", "CA", "MA"),
Sales = c(15, 100, 30, 56, 60),
Price = c(34, 52, 21, NA, 20),
stringsAsFactors = FALSE)
Now I want a function that creates a new data.frame
that just has a given state... So I wrote:
state_function <- function(state) {
#Subset the rows in the state from all rows
new_df <- df[df[,"State"] == state, ]
## I will do many other things inside the function ##
summary(new_df)
return(new_df)
}
state_function ("NJ")
But now I want to re-use this data.frame
, new_df
, outside of this function ..(and I want to generalize this example) .. How can I do this? In general, I want to use a object or data.frame
that I create in a function, outside of a function.
Upvotes: 2
Views: 1985
Reputation: 99331
You can also use assign
to assign a global variable within a function. Be careful though, as y
will be overwritten each time you run the function.
state.function <- function(x, state)
{
assign('y', subset(x, x$State == state), envir = .GlobalEnv)
return(y)
}
> state.function(df, "CA")
# State Sales Price
# 3 CA 30 21
# 4 CA 56 NA
> y
# State Sales Price
# 3 CA 30 21
# 4 CA 56 NA
This works well if you're going through data quickly. If you remove return(y)
there will be no output to the function, but the variable y
will exist in the global environment. This is sometimes useful.
And if you hate quoting things, like I do
state.function <- function(x, state)
{
z <- deparse(substitute(state))
assign('y', subset(x, x$State == z), envir = .GlobalEnv)
}
> state.function(df, CA)
> y
# State Sales Price
# 3 CA 30 21
# 4 CA 56 NA
Upvotes: 0
Reputation: 269481
I think you want this:
state_function <- function(state) {
new_df <- subset(df, State == state)
print( summary(new_df) )
new_df
}
new_df <- state_function("CA")
Note that we can use the subset
function to clean up the first line of the body. Also the summary
line in the question does nothing without print
. Automatic printing does not occur within functions. Also the return
is not really needed. Finally assign the output of the function to a variable.
Upvotes: 1
Reputation: 42639
Assign the value to a variable when running the function:
new_df <- state_function("NJ")
Upvotes: 2