简体   繁体   中英

Apply a function to each row of a column in a dataframe to create a new column

I'm having trouble applying a function to each row within a column of a dataframe to create a new column, and would appreciate some guidance. The function is question is a maxplus operator

maxplus  <- function(x, lb=0){max(x, lb)}

I have a dataframe r_df which looks like this:

head(r_df)

      Date    GS3M_ret
1 Jan 1990 0.006583333
2 Feb 1990 0.006666667
3 Mar 1990 0.006808333
4 Apr 1990 0.006700000
5 May 1990 0.006675000
6 Jun 1990 0.006658333

I want to apply maxplus to the last column to get a positive restriction of GS3M_ret. Using the operator directly gives me a single number, which is not what i want (i want each row of test to be the maximum of the corresponding row of GS3M_ret and 0)

maxplus(r_df[, "GS3M_ret"], 0)

        Date     GS3M_ret        test
1   Jan 1990 6.583333e-03 0.006808333
2   Feb 1990 6.666667e-03 0.006808333
3   Mar 1990 6.808333e-03 0.006808333
4   Apr 1990 6.700000e-03 0.006808333
5   May 1990 6.675000e-03 0.006808333'

I have tried apply, tapply, sapply, etc. and get a variant of the following

apply(r_df[, "GS3M_ret"], 1, maxplus)

Error in apply(r_df[, "GS3M_ret"], 1, maxplus): dim(X) must have a positive length

or

tapply(r_df[, "GS3M_ret"], 1, maxplus)

Error in tapply(r_df[, "GS3M_ret"], 1, maxplus): arguments must have same length

I'm clearly doing some wrong (and wrong in an elementary way), but haven't been able to solve my problem. Any assistance would be greatly appreciated.

max returns a single number. To test for each number separately use pmax .

maxplus  <- function(x, lb=0) pmax(x, lb)

maxplus(df$GS3M_ret)
#Or specify lb
#maxplus(df$GS3M_ret, 0)
#[1] 0.006583333 0.006666667 0.006808333 0.006700000 0.006675000 0.006658333

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM