是否有R函数将矢量应用于DF中的列并创建新列?
我正在使用tidyverse
对房价数据集进行一些分析。随着时间的流逝,我有房价,我想通过应用下降付款的向量来为每个首付百分比输出新列来创建新的专栏。
set.seed(100)
dates <- seq.Date(from = as.Date('2010-01-01'), to = as.Date('2010-12-01'), by = 'months')
prices <- rnorm(12, mean = 100000, sd = 5000)
home_vals <- data.frame(dates, prices)
out <- home_vals %>%
mutate(
DownPayment_20 = 0.2 * prices
,DownPayment_10 = 0.1 * prices
,DownPayment_5 = 0.05 * prices
)
head(out)
dates prices DownPayment_20 DownPayment_10 DownPayment_5
1 2010-01-01 97489.04 19497.81 9748.904 4874.452
2 2010-02-01 100657.66 20131.53 10065.766 5032.883
3 2010-03-01 99605.41 19921.08 9960.541 4980.271
4 2010-04-01 104433.92 20886.78 10443.392 5221.696
5 2010-05-01 100584.86 20116.97 10058.486 5029.243
6 2010-06-01 101593.15 20318.63 10159.315 5079.658
我希望通过仅通过down_payments&lt; - c(0.2,0.1,0.05)的向量来执行此操作,但我无法弄清楚一个直接执行此操作的方法,而无需手动突变每一列。此外,我可能想在某些计算中包括其他带有输入的向量,并同时使用新的付款向量和新向量,并为该金额提供新的列。
谢谢!
I am using tidyverse
to do some analysis to a dataset of home prices. I have home prices over time, and I would like to create new columns by applying a vector of down payments to output a new column for each down payment percentage.
set.seed(100)
dates <- seq.Date(from = as.Date('2010-01-01'), to = as.Date('2010-12-01'), by = 'months')
prices <- rnorm(12, mean = 100000, sd = 5000)
home_vals <- data.frame(dates, prices)
out <- home_vals %>%
mutate(
DownPayment_20 = 0.2 * prices
,DownPayment_10 = 0.1 * prices
,DownPayment_5 = 0.05 * prices
)
head(out)
dates prices DownPayment_20 DownPayment_10 DownPayment_5
1 2010-01-01 97489.04 19497.81 9748.904 4874.452
2 2010-02-01 100657.66 20131.53 10065.766 5032.883
3 2010-03-01 99605.41 19921.08 9960.541 4980.271
4 2010-04-01 104433.92 20886.78 10443.392 5221.696
5 2010-05-01 100584.86 20116.97 10058.486 5029.243
6 2010-06-01 101593.15 20318.63 10159.315 5079.658
I'd like to be able to do this for any number of down payment inputs, by just passing a vector like down_payments <- c(0.2, 0.1, 0.05)
, but I cannot figure out a straightforward way to do this without manually mutating every column. Additionally, I might want to include other vectors with inputs, and use both the down payment vector and the new vector in some calculation, and have a new column for that amount.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在基本中,r使用
外部
函数或简单地%o%
in Base R use the
outer
function or simply%o%
怎么样:
由
How about this:
Created on 2022-06-30 by the reprex package (v2.0.1)
使用
data.table
:With
data.table
: