Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 与不同ID关联的查找和求和值_Arrays_R_Dataframe_Lookup_Data Manipulation - Fatal编程技术网

Arrays 与不同ID关联的查找和求和值

Arrays 与不同ID关联的查找和求和值,arrays,r,dataframe,lookup,data-manipulation,Arrays,R,Dataframe,Lookup,Data Manipulation,我有一个文件,其中包含与ID的每个唯一值相关联的depression的a值。名为HAVE的数据框如下所示: id depression friendid_A friendid_B friendid_C friendid_D 1 1.0 NA 3 6 5 2 0.6 6 4 NA NA 3 0.0

我有一个文件,其中包含与ID的每个唯一值相关联的depression的a值。名为HAVE的数据框如下所示:

id  depression friendid_A friendid_B friendid_C friendid_D
1          1.0         NA          3          6          5
2          0.6          6          4         NA         NA
3          0.0          1          4          5         NA
4          1.8          1          3         NA          2
5          1.7         NA         NA         NA         NA
6          0.3          2          3         NA         NA 
我想添加一个变量depression\u sum,用于查找观察中列出的每个ID的depression值,并将其相加。例如,第一个观测值包括ID 3、6和5,用于其各种friendid\n变量。这三个ID的抑郁值分别为0.0、0.3和1.7。因此,这一观察结果的压差总和为2.0

下面是我想创建的名为“想要”的数据框:

id  depression  friendid_A  friendid_B  friendid_C  friendid_D  depression_sum
1          1.0          NA           3           6           5            2.0
2          0.6           6           4          NA          NA            2.1
3          0.0           1           4           5          NA            4.5
4          1.8           1           3          NA           2            1.6
5          1.7          NA          NA          NA          NA             NA
6          0.3           2           3          NA          NA            0.6

有没有办法有效地查找这些值并创建一个包含其总和的变量?

tidyverse解决方案

library(tidyverse)

WANT <- HAVE %>% 
  gather(key, value, -id, -depression, na.rm = TRUE) %>%
  group_by(id) %>%
  summarize(
    depression_sum = sum(HAVE$depression[match(value, HAVE$id)])
  ) %>%
  left_join(HAVE, .)
库(tidyverse)
想要%
聚集(键,值,-id,-depression,na.rm=TRUE)%>%
分组依据(id)%>%
总结(
萧条_sum=sum(拥有$depression[匹配(值,拥有$id)])
) %>%
左联

HAVE可以通过添加列来修改
HAVE
data.frame本身。也许,需要创建另一个数据帧。
WANT
(由
OP
指定)可以避免

base-R
中使用
apply
的解决方案:

HAVE$depression_sum <- apply(df[3:nrow(df)], 1,
            function(x)sum(df$depression[HAVE$id %in% x], na.rm = TRUE))

HAVE
#   id depression friendid_A friendid_B friendid_C friendid_D depression_sum
# 1  1        1.0         NA          3          6          5            2.0
# 2  2        0.6          6          4         NA         NA            2.1
# 3  3        0.0          1          4          5         NA            4.5
# 4  4        1.8          1          3         NA          2            1.6
# 5  5        1.7         NA         NA         NA         NA            0.0
# 6  6        0.3          2          3         NA         NA            0.6

HAVE$depression\u sum您可能希望将列重命名为
friendid\u a、\u b、\u c、\u d
,以避免混淆刚刚完成的操作-谢谢!
HAVE$depression_sum <- apply(df[3:nrow(df)], 1,
            function(x)sum(df$depression[HAVE$id %in% x], na.rm = TRUE))

HAVE
#   id depression friendid_A friendid_B friendid_C friendid_D depression_sum
# 1  1        1.0         NA          3          6          5            2.0
# 2  2        0.6          6          4         NA         NA            2.1
# 3  3        0.0          1          4          5         NA            4.5
# 4  4        1.8          1          3         NA          2            1.6
# 5  5        1.7         NA         NA         NA         NA            0.0
# 6  6        0.3          2          3         NA         NA            0.6