[英]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.我在将 function 应用于 dataframe 的列中的每一行以创建新列时遇到问题,希望得到一些指导。 The function is question is a maxplus operator function 是一个 maxplus 运算符的问题
maxplus <- function(x, lb=0){max(x, lb)}
I have a dataframe r_df
which looks like this:我有一个 dataframe r_df
,它看起来像这样:
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.我想将maxplus
应用于最后一列以获得 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)使用运算符直接给我一个数字,这不是我想要的(我希望每一行测试都是 GS3M_ret 和 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我已经尝试过应用、tapply、sapply 等,并得到以下的变体
apply(r_df[, "GS3M_ret"], 1, maxplus)
Error in apply(r_df[, "GS3M_ret"], 1, maxplus): dim(X) must have a positive length apply(r_df[, "GS3M_ret"], 1, maxplus 出错:dim(X) 必须有正长度
or要么
tapply(r_df[, "GS3M_ret"], 1, maxplus)
Error in tapply(r_df[, "GS3M_ret"], 1, maxplus): arguments must have same length tapply 中的错误(r_df[,“GS3M_ret”],1,maxplus):arguments 必须具有相同的长度
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. max
返回单个数字。 To test for each number separately use pmax
.要分别测试每个数字,请使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.