Excel像R数据框中的列操作

编程入门 行业动态 更新时间:2024-10-27 10:26:10
本文介绍了Excel像R数据框中的列操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试复制我在Excel中使用R的一些建模。我已经将数据从csv文件读入数据帧。 csv文件包含两列数据 A 和 B 。

我现在要添加其他列 C , D 和 E 到数据帧,并填充新列C,D和E,其中通过将公式应用于以前的列。

下面的代码片段应该有助于澄清我要做的事情:

ABCDE 1 100.5 101.3 2 102.6 102.5 3 107.2 109.3 4 99.3 89.3 5 102.8 100.7 =(B5-B1)* A5 = C5 = IF(D5> 100,1,-1) 6 107.2 98.9 =(B6-B2)* A6 = C6 + C5 = IF(D6> 100,1,-1) 7 99.8 109.9 =(B7-B3)* A7 = C7 + C6 = IF(D7> 100,1,-1) 8 108.2 99.5 =(B8-B4)* A8 = C8 + C7 = (D8> 100,1,-1) 9 78.7 89.6 =(B9-B5)* A9 = C9 + C8 = IF(D9> 100,1,-1) 10 108.9 109.2 =(B10-B6)* A10 = C10 + C9 = IF(D10> 100,1,-1)

如何复制Excel(ahem - Excels)中使用的这种柱状函数式编程?

解决方案

我的大脑正在这样做抗议。它让我觉得我回到了Minitab会议。

dfrm $ C< - NA dfrm $ C [5:10]< - 与(dfrm,(B [5:10] -B [1:6])* A [5:10]) dfrm $ D< - NA dfrm $ D [5:10]< - cumsum(dfrm $ C [5:10]) dfrm $ E < - NA dfrm $ E [5:10] 1 - 2 *(dfrm $ D [5:10]< = 100)#也可以使用ifelse() dfrm ABCDE 1 100.5 101.3 NA NA NA 2 102.6 102.5 NA NA NA 3 107.2 109.3 NA NA NA 4 99.3 89.3 NA NA NA 5 102.8 100.7 -61.68 -61.68 -1 6 107.2 98.9 - 385.92 -447.60 -1 7 99.8 109.9 59.88 -387.72 -1 8 108.2 99.5 1103.64 715.92 1 9 78.7 89.6 -873.57 -157.65 -1 10 108.9 109.2 1121.67 964.02 1

I am trying to replicate some modelling I did in Excel, using R. I have read the data from a csv file into a dataframe. The csv file contains two columns of data A and B.

I now want to add additional columns C, D and E to the dataframe and to populate the new columns C, D and E with data generated by applying a formula to the data in the previous columns.

The snippet below should hopefully clarify what I'm trying to do:

A B C D E 1 100.5 101.3 2 102.6 102.5 3 107.2 109.3 4 99.3 89.3 5 102.8 100.7 =(B5-B1)*A5 = C5 = IF(D5 >100,1,-1) 6 107.2 98.9 =(B6-B2)*A6 = C6+C5 = IF(D6 >100,1,-1) 7 99.8 109.9 =(B7-B3)*A7 = C7+C6 = IF(D7 >100,1,-1) 8 108.2 99.5 =(B8-B4)*A8 = C8+C7 = IF(D8 >100,1,-1) 9 78.7 89.6 =(B9-B5)*A9 = C9+C8 = IF(D9 >100,1,-1) 10 108.9 109.2 =(B10-B6)*A10 = C10+C9 = IF(D10 >100,1,-1)

How can I replicate this kind of "columnar" functional programming that Excel (ahem - Excels) in - using R?

解决方案

My brain is doing this under protest. It makes me feel that I'm back at a Minitab session.

dfrm$C <- NA dfrm$C[5:10] <- with(dfrm, (B[5:10]-B[1:6])*A[5:10]) dfrm$D <- NA dfrm$D[5:10] <- cumsum(dfrm$C[5:10]) dfrm$E <- NA dfrm$E[5:10] <- 1 - 2*(dfrm$D[5:10] <= 100) # could also use ifelse() dfrm A B C D E 1 100.5 101.3 NA NA NA 2 102.6 102.5 NA NA NA 3 107.2 109.3 NA NA NA 4 99.3 89.3 NA NA NA 5 102.8 100.7 -61.68 -61.68 -1 6 107.2 98.9 -385.92 -447.60 -1 7 99.8 109.9 59.88 -387.72 -1 8 108.2 99.5 1103.64 715.92 1 9 78.7 89.6 -873.57 -157.65 -1 10 108.9 109.2 1121.67 964.02 1

更多推荐

Excel像R数据框中的列操作

本文发布于:2023-10-25 03:20:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   操作   数据   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!