简体   繁体   English

将 function 应用于 dataframe 中的列的每一行以创建新列

[英]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.

相关问题 R将函数应用于数据框的每一行,将结果存储在同一数据框的新列中 - R apply function to each row of dataframe, store result in new column of same dataframe 将函数应用于每列数据框 - Apply function to each column of dataframe 在 R 中,如何在使用列值的每个 dataframe 行上应用 function? - In R, how to apply a function on each dataframe row that uses a column value? Sparklyr / Dplyr-如何为sparkdata框架的每一行应用用户定义的函数,以及如何将每一行的输出写入新列? - Sparklyr/Dplyr - How to apply a user defined function for each row of a sparkdata frame and create write the output of each row to new column? 尝试将函数rowwise应用于数据框以创建新列 - Trying to apply a function rowwise to a dataframe to create a new column 将函数应用于数据框中的每一行并将结果存储在新列中 - Applying function to each row in dataframe and storing result in new column 将函数应用于R中数据框的列中的每个单元格 - apply a function to each cell in a column of a dataframe in R 如何将 function 应用于 R 的 dataframe 中的每一列 - how to apply function to each column in dataframe of R 如何将 lme function 应用于 dataframe 的每一列? - how to apply lme function to each column of dataframe? 创建一个新的 dataframe 显示每列的总和 - Create a new dataframe showing the sum of each column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM