Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dplyr对R中不同列(和不同行位置)的行执行操作_R_Dplyr - Fatal编程技术网

使用dplyr对R中不同列(和不同行位置)的行执行操作

使用dplyr对R中不同列(和不同行位置)的行执行操作,r,dplyr,R,Dplyr,这是我的数据帧: structure(list(Year = c(1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1976L, 1976L), Month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L), A = c(0.419727177, 0.411522634, 0.415627598,

这是我的数据帧:

structure(list(Year = c(1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 
1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1976L, 1976L), Month = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L), A = c(0.419727177, 
0.411522634, 0.415627598, 0.425350915, 0.431778929, 0.455892409, 
0.464252553, 0.473933649, 0.48947626, 0.481231954, 0.495049505, 
0.49419323, 0.4927322, 0.493778392), log.S.grb = c(-0.86815035734372, 
-0.887891256732457, -0.877965616764487, -0.854840768392932, -0.839841560146749, 
-0.785498442482632, -0.767326579572197, -0.746687948097975, -0.714419316655485, 
-0.731405892265036, -0.703097511313113, -0.704828684428783, -0.707789457396815, 
-0.705668461635217), D = c(10.2641, 9.7704, 9.3694, 9.2403, 9.4459, 
9.4826, 10.4272, 10.3805, 10.4835, 11.4103, 10.988, 10.7708, 
9.2987, 8.6161), E = c(5.68, 5.4, 5.53, 5.5, 5.2, 5.86, 6.25, 
6.36, 6.58, 5.51, 5.54, 5.2, 4.73, 5), J = c(1.15663289, 1.05923536, 
0.938740721, 0.890710069, 1.012043355, 0.843618397, 0.850583558, 
0.957856493, 0.888553262, 1.391339534, 1.309574432, 1.322714922, 
1.247746749, 0.894350421), st3_st.grb = c(NA, NA, NA, 1.33095889507878, 
4.80496965857083, 9.24671742818556, 8.75141888207357, 9.31536120487736, 
7.10791258271466, 3.59206873071611, 4.35904367848617, 0.959063222670167, 
2.36164348682203, -0.257095032210358), idiff_3m.grb = c(1.08206512614859, 
1.03439401057093, 0.909556730440464, 0.886569121738501, 1.00676358283349, 
0.856864819463321, 0.983497311932147, 0.946390153374566, 0.917819573656709, 
1.38813518035337, 1.28343598984861, 1.31491300418167, 1.08566149047631, 
0.860507535403032), EXCESS = c("NA", "NA", "NA", "NA", "NA", 
"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA")), row.names = c(NA, 
14L), class = "data.frame")
我想要的是通过使用
dplyr
(可能
mutate
函数)执行以下计算:在多余的列中,将是
(idiff_3m.grb-st3_st.grb)
操作的值,但是
idiff_3m.grb
的第一个值将是第三个值,
st3_st.grb
的第一个值将是第一个值。结果将在多余列的第三个位置开始填充,依此类推

换句话说,第一个操作是:

(1.0820651 - 1.3309589) = -0.2488938 = EXCESS[3,] This first result will be in the third position of EXCESS column.
最后,我将有三个职位在多余的列中由
“NA”
填充(前三个)


有什么帮助吗?

您可以使用
lag
功能移动另一列,然后执行计算:

library(dplyr)

df %>%
  mutate(idiff_3m.grb_lag3 = lag(idiff_3m.grb, 3),
         EXCESS = idiff_3m.grb_lag3 - st3_st.grb)
返回:


如果需要,您可以删除滞后列,或者使用
df%>%mutate(EXCESS=lag(idiff_3m.grb,3)-st3_st.grb)

我不完全清楚您的预期输出。我假设您要的是
超额[I]=idiff_3m.grb[I]-st3_st.grb[I+3]

library(tidyverse)
df %>% mutate(EXCESS = idiff_3m.grb - lead(st3_st.grb, n = 3))
#    Year Month         A  log.S.grb       D    E         J st3_st.grb
# 1  1975     1 0.4197272 -0.8681504 10.2641 5.68 1.1566329         NA
# 2  1975     2 0.4115226 -0.8878913  9.7704 5.40 1.0592354         NA
# 3  1975     3 0.4156276 -0.8779656  9.3694 5.53 0.9387407         NA
# 4  1975     4 0.4253509 -0.8548408  9.2403 5.50 0.8907101  1.3309589
# 5  1975     5 0.4317789 -0.8398416  9.4459 5.20 1.0120434  4.8049697
# 6  1975     6 0.4558924 -0.7854984  9.4826 5.86 0.8436184  9.2467174
# 7  1975     7 0.4642526 -0.7673266 10.4272 6.25 0.8505836  8.7514189
# 8  1975     8 0.4739336 -0.7466879 10.3805 6.36 0.9578565  9.3153612
# 9  1975     9 0.4894763 -0.7144193 10.4835 6.58 0.8885533  7.1079126
# 10 1975    10 0.4812320 -0.7314059 11.4103 5.51 1.3913395  3.5920687
# 11 1975    11 0.4950495 -0.7030975 10.9880 5.54 1.3095744  4.3590437
# 12 1975    12 0.4941932 -0.7048287 10.7708 5.20 1.3227149  0.9590632
# 13 1976     1 0.4927322 -0.7077895  9.2987 4.73 1.2477467  2.3616435
# 14 1976     2 0.4937784 -0.7056685  8.6161 5.00 0.8943504 -0.2570950
#    idiff_3m.grb      EXCESS
# 1     1.0820651 -0.24889377
# 2     1.0343940 -3.77057565
# 3     0.9095567 -8.33716070
# 4     0.8865691 -7.86484976
# 5     1.0067636 -8.30859762
# 6     0.8568648 -6.25104776
# 7     0.9834973 -2.60857142
# 8     0.9463902 -3.41265353
# 9     0.9178196 -0.04124365
# 10    1.3881352 -0.97350831
# 11    1.2834360  1.54053102
# 12    1.3149130          NA
# 13    1.0856615          NA
# 14    0.8605075          NA

带有
data.table的选项

library(data.table)
setDT(df1)[, EXCESS := shift(idiff_3m.grb, n = 3) - st3_st.grb]
df1
#    Year Month         A  log.S.grb       D    E         J st3_st.grb idiff_3m.grb      EXCESS
# 1: 1975     1 0.4197272 -0.8681504 10.2641 5.68 1.1566329         NA    1.0820651          NA
# 2: 1975     2 0.4115226 -0.8878913  9.7704 5.40 1.0592354         NA    1.0343940          NA
# 3: 1975     3 0.4156276 -0.8779656  9.3694 5.53 0.9387407         NA    0.9095567          NA
# 4: 1975     4 0.4253509 -0.8548408  9.2403 5.50 0.8907101  1.3309589    0.8865691 -0.24889377
# 5: 1975     5 0.4317789 -0.8398416  9.4459 5.20 1.0120434  4.8049697    1.0067636 -3.77057565
# 6: 1975     6 0.4558924 -0.7854984  9.4826 5.86 0.8436184  9.2467174    0.8568648 -8.33716070
# 7: 1975     7 0.4642526 -0.7673266 10.4272 6.25 0.8505836  8.7514189    0.9834973 -7.86484976
# 8: 1975     8 0.4739336 -0.7466879 10.3805 6.36 0.9578565  9.3153612    0.9463902 -8.30859762
# 9: 1975     9 0.4894763 -0.7144193 10.4835 6.58 0.8885533  7.1079126    0.9178196 -6.25104776
#10: 1975    10 0.4812320 -0.7314059 11.4103 5.51 1.3913395  3.5920687    1.3881352 -2.60857142
#11: 1975    11 0.4950495 -0.7030975 10.9880 5.54 1.3095744  4.3590437    1.2834360 -3.41265353
#12: 1975    12 0.4941932 -0.7048287 10.7708 5.20 1.3227149  0.9590632    1.3149130 -0.04124365
#13: 1976     1 0.4927322 -0.7077895  9.2987 4.73 1.2477467  2.3616435    1.0856615 -0.97350831
#14: 1976     2 0.4937784 -0.7056685  8.6161 5.00 0.8943504 -0.2570950    0.8605075  1.54053102

你能再举一个例子吗?你怎么能用一个值填充第三个多余的位置,然后说你希望前三个是NAs?是的!我如何处理从第三个位置开始的
多余
列的值?@DiogoBastos看一看AntoniosK的答案。那可能就是你想要的?在他的回答中,
oversed[i]=idiff_3m.grb[i-3]-st3_st.grb[i]
。我不知道你想要什么。非常欢迎你@DiogoBastos;很高兴它成功了。
library(data.table)
setDT(df1)[, EXCESS := shift(idiff_3m.grb, n = 3) - st3_st.grb]
df1
#    Year Month         A  log.S.grb       D    E         J st3_st.grb idiff_3m.grb      EXCESS
# 1: 1975     1 0.4197272 -0.8681504 10.2641 5.68 1.1566329         NA    1.0820651          NA
# 2: 1975     2 0.4115226 -0.8878913  9.7704 5.40 1.0592354         NA    1.0343940          NA
# 3: 1975     3 0.4156276 -0.8779656  9.3694 5.53 0.9387407         NA    0.9095567          NA
# 4: 1975     4 0.4253509 -0.8548408  9.2403 5.50 0.8907101  1.3309589    0.8865691 -0.24889377
# 5: 1975     5 0.4317789 -0.8398416  9.4459 5.20 1.0120434  4.8049697    1.0067636 -3.77057565
# 6: 1975     6 0.4558924 -0.7854984  9.4826 5.86 0.8436184  9.2467174    0.8568648 -8.33716070
# 7: 1975     7 0.4642526 -0.7673266 10.4272 6.25 0.8505836  8.7514189    0.9834973 -7.86484976
# 8: 1975     8 0.4739336 -0.7466879 10.3805 6.36 0.9578565  9.3153612    0.9463902 -8.30859762
# 9: 1975     9 0.4894763 -0.7144193 10.4835 6.58 0.8885533  7.1079126    0.9178196 -6.25104776
#10: 1975    10 0.4812320 -0.7314059 11.4103 5.51 1.3913395  3.5920687    1.3881352 -2.60857142
#11: 1975    11 0.4950495 -0.7030975 10.9880 5.54 1.3095744  4.3590437    1.2834360 -3.41265353
#12: 1975    12 0.4941932 -0.7048287 10.7708 5.20 1.3227149  0.9590632    1.3149130 -0.04124365
#13: 1976     1 0.4927322 -0.7077895  9.2987 4.73 1.2477467  2.3616435    1.0856615 -0.97350831
#14: 1976     2 0.4937784 -0.7056685  8.6161 5.00 0.8943504 -0.2570950    0.8605075  1.54053102