基于R中以前的变量值将行附加到数据帧的功能 [英] Function for Appending Rows to Data Frame Based on Previous Variable Values in R

查看:131
本文介绍了基于R中以前的变量值将行附加到数据帧的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,允许我根据最上一行的变量(紧接在上面)多次将行附加到现有的数据框中。

I'm trying to create a function that allows me to append rows to an existing data frame based on variables from the most previous row (immediately above), multiple times over.

#Here's what I'm starting with 

Balance <- c(25000)
Pmt <- c(1500)
Interest <- c(.05)
DF <- data.frame(Balance,Pmt,Interest)
DF  
   Balance Pmt Interest
1   25000 100     0.05

例如,我想看看新行的余额等于前一行的((Balance-Pmt)*(1 + Inerest))的下一行4行,Pmt和兴趣行保持不变。

For example, I'd like to see the next 4 rows added where the "Balance" of the new row is equal to ((Balance-Pmt)*(1+Inerest)) of the previous row, and the "Pmt" and "Interest" rows stay constant.

# Manually
DF[2,1] <- (DF[1,1] - DF[1,2])*(1+DF[1,3])
DF[2,2] <- DF[1,2]
DF[2,3] <- DF[1,3]
D
  Balance  Pmt Interest
1   25000 1500     0.05
2   24675 1500     0.05

显然,我想像这样复制n次,而不需要手动引用上一行。我想要一个函数,允许我按照同样的逻辑,按照我想要的方式向数据框添加行。任何帮助不胜感激!

Obviously, I'd like to replicate this as many times, n, as I'd like without having to manually reference the previous row. I'd like a function that allows me to add rows to the data frame as many times as I'd like, following that same logic. Any help is appreciated!

推荐答案

而不是逐行构建数据框架,这将非常低效,您可以设置数据框架以确定最终尺寸。在这个例子中,你知道你将运行这个过程三次,总共四行结束。使数据框首先使其余部分更容易和更快速:

Instead of building the data frame line by line which will be very inefficient, you can setup the data frame to have the end size in mind. In this example you know you will run this process three times to end with four rows total. Make that data frame first to make the rest easier and quicker:

DF2 <- 'row.names<-'(DF[rep(1,4),], NULL)
DF2$Balance <- Reduce(function(x,y) (x - y)*(1+DF2[1,3]), DF2[-1,2], init=DF[1,1], acc=TRUE)
DF2
#    Balance  Pmt Interest
# 1 25000.00 1500     0.05
# 2 24675.00 1500     0.05
# 3 24333.75 1500     0.05
# 4 23975.44 1500     0.05






自学:如何减少工作?

减少是一个更高级别的功能,以独特的方式工作,需要练习掌握。它包含两个主要部分:1)一个具有两个参数的函数,2)一个向量*。这是奇怪的部分。它需要一个具有两个args和一个向量的函数。起初没有意义。如何通过一个需要两个参数的函数的向量?示例:

Reduce is a higher level function that works in a unique way that takes practice to master. It takes in two main pieces, 1) a function with two arguments and 2) a single vector*. That's the weird part. It wants a function with two args and one vector. That doesn't make sense at first. How does it go through one vector with a function that needs two args? Example:

#one vector
x <- c(2,3,4,5)

#function with two arguments
multiply <- function(a, b)  {a * b}

这是从2到5的向量,一个简单的函数需要两个数字并将它们相乘。 减少将接受这两个对象,并执行以下操作:

This is a vector from 2 to 5, and a simple function that takes two numbers and multiplies them. Reduce will accept these two objects and do this:

Reduce(multiply, x) #<- this
#is the same as
ans1 <- multiply(2, 3) 
ans2 <- multiply(ans1, 4)
ans3 <- multiply(ans2, 5)

它通过向量 c (2,3,4,5)并取得前两部分(2和3),并对其进行调用。然后它拿出了这个答案,并带来了x(4)的第三个元素来运行该函数,然后用第四个元素(5)运行该答案。

It went through the vector c(2,3,4,5) and took the first two parts (2 and 3) and called the function on them. Then it took that answer and brought in the third element of x (4) to run the function, then it took that answer and ran it with the fourth element (5).

在你的例子中,我们用它来完成第一笔余额和付款:

In your example, we used it to go through the first balance and the payments:

#one vector
x <- c(25000, 1500, 1500, 1500)

#function with two arguments
f <- function(a,b) (a - b)*(1 + 0.05)

让我们看看内部的内容:


Let's see what it did internally:

Reduce(f, x) #what we did
#internally Reduce did this
ans1 <- (25000 - 1500) * (1 + 0.05)
ans2 <- (ans1 - 1500) * (1 + 0.05)
ans3 <- (ans2 - 1500) * (1 + 0.05)

这给了我们所需的过程。那就是我们做了什么的想法。为了完全完成我们的讨论,我们添加了两个额外的参数,帮助我们获得了预期的结果。 accumulate = TRUE init = DF [1,1] 。第一个简单的说,减少,我们想要每个连续的答案,而不是最后一个答案。第二个告诉减少我们要开始的价值。要显示什么 init 做了:

This gave us the desired process. That is the idea of what we did. To totally complete our discussion, we added two extra arguments that helped us to get the desired result. accumulate=TRUE and init=DF[1,1]. The first simply tells Reduce that we want each successive answer instead of just the last one. The second tells Reduce what value we want to start with. To show what init did:

#Supply vector for function
x <- DF[-1,2]
x
[1] 1500 1500 1500

#We need the initial balance
#init=DF[1,1] does: 
[1] 25000 1500 1500 1500

减少可以还要列出一个列表,但是我们应该首先实现向量精通,然后介绍列表方法。

这篇关于基于R中以前的变量值将行附加到数据帧的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆