Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
R循环生成新向量_R_Loops_Sapply - Fatal编程技术网

R循环生成新向量

R循环生成新向量,r,loops,sapply,R,Loops,Sapply,这是我的数据样本。我希望分数变量的总体加权计数如下: data = data.frame("id"=c(1,2,3,4,5,6,7,8,9,10), "group"=c(1,1,2,1,2,2,2,2,1,2), "type"=c(1,1,2,3,2,2,3,3,3,1), "score1"=c(sample(1:4,10,r=T)), "score2"

这是我的数据样本。我希望分数变量的总体加权计数如下:

data = data.frame("id"=c(1,2,3,4,5,6,7,8,9,10),
                  "group"=c(1,1,2,1,2,2,2,2,1,2),
                  "type"=c(1,1,2,3,2,2,3,3,3,1),
                  "score1"=c(sample(1:4,10,r=T)),
                  "score2"=c(sample(1:4,10,r=T)),
                  "score3"=c(sample(1:4,10,r=T)),
                  "score4"=c(sample(1:4,10,r=T)),
                  "score5"=c(sample(1:4,10,r=T)),
                  "weight1"=c(173,109,136,189,186,146,173,102,178,174),
                  "weight2"=c(147,187,125,126,120,165,142,129,144,197),
                  "weight3"=c(103,192,102,159,128,179,195,193,135,145),
                  "weight4"=c(114,182,199,101,111,116,198,123,119,181),
                  "weight5"=c(159,125,104,171,166,154,197,124,180,154))
然而,我的目标是创建一个类型的循环,这样,我可以对分数1-5的“组”和“类型”的每个组合执行此操作,并将它们存储在单独的向量中,以便

count(data, score1, wt = weight1)
count(data, score2, wt = weight2)
count(data, score3, wt = weight3)
count(data, score4, wt = weight4)
count(data, score5, wt = weight5)

等等。

我不确定您的预期输出是什么,但您可能希望尝试以下内容:

vec1 = weighted score variable for scores1-5 for group = 1 and type = 1
vec2 = weighted score variable for scores1-5 for group = 1 and type = 2
vec3 = weighted score variable for scores1-5 for group = 1 and type = 3
for(i in 1:max(data[[“group”]]){#在组中循环

加权评分我们可以使用
map
循环遍历每个相应的“评分”、“权重”,并获得
计数

for (i in 1:max(data[["group"]])) { #looping through groups
  weighted_score <- ... ## create your wheighted score for group i here

  name <- paste("vec",i,sep="")
  assign(name,weighted_score)

}
库(tidyverse)
超出%
选择(组、类型、匹配项(作为.character(.x)))%>%
分组依据(组,类型)%>%
计数(!!rlang::sym(str_c(“score”,.x)),
wt=!!rlang::sym(str_c(“重量“,.x)))

输出将是频率
计数
列表
TIBLE
。如果要创建单个数据,请使用
映射df
.id

您的预期输出是什么样的?6个向量(对于每个组和类型组合),每个向量包含5个使用计数()估计的数字如示例所示,最好有一个set.seed,并且至少对一个组合有预期的输出。您显示了
count(data,score1,wt=weight1)
这是我使用的类似逻辑谢谢你,但为什么我要设置一个无种子是随机的?@bvowe我想你有
样本
我需要循环通过组*类型。另外,对于加权的_分数你好,实际上应该有6个向量(每个组*类型一个),每个向量的长度应该是5(每个分数)这有意义吗???@bvowe我的代码基于您在帖子中提供的代码片段。不清楚。我很抱歉。我有六个组(2组*3种类型)。对于每个组,我的目标是估计分数1到分数5的样本加权计数。这应该会产生总共6个向量(每组一个)每个向量应该包含5个值(每个分数一个)@bvowe在这种情况下,只需在“计数”中添加
类型
。更新我相信你的代码可以工作,但是如果数据长而不是宽呢?
library(tidyverse)
out <- map(1:5, ~ 
       data %>%
         select(group, type, matches(as.character(.x))) %>% 
         group_by(group, type) %>%
         count(!! rlang::sym(str_c("score", .x)), 
         wt = !! rlang::sym(str_c("weight", .x))))