Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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_Function_Loops_Vector_Percentile - Fatal编程技术网

R 将值从矢量映射到数据框:计算百分位

R 将值从矢量映射到数据框:计算百分位,r,function,loops,vector,percentile,R,Function,Loops,Vector,Percentile,我有一个数字向量(nth\RT)和一个数据帧(df): 我想计算并添加每个受试者每个区块的第n个百分位数作为新列(n),即第1区块第一名受试者RTs的第61百分位数,第2区块第一名受试者RTs的第47百分位数,第3区块第一名受试者RTs的第50百分位数,第1区块第2名受试者的第53百分位数,等等。因此数据框将如下所示: df # Subject RT Trial Block Rank nth #1 1 234 1 1 1 310.28

我有一个数字向量(nth\RT)和一个数据帧(df):

我想计算并添加每个受试者每个区块的第n个百分位数作为新列(n),即第1区块第一名受试者RTs的第61百分位数,第2区块第一名受试者RTs的第47百分位数,第3区块第一名受试者RTs的第50百分位数,第1区块第2名受试者的第53百分位数,等等。因此数据框将如下所示:

df
#    Subject    RT Trial Block  Rank  nth
#1        1   234     1     1     1   310.28
#2        1   239     3     1     2   310.28
#3        1   563     2     1     3   310.28
#4        1   230     1     2     1   233.76
#5        1   234     3     2     2   233.76
#6        1   467     2     2     3   233.76
#7        1   111     3     3     1   466
#8        1   466     2     3     2   466
#9        1   543     1     3     3   466
#10       2    44     2     1     1   230.2
#11       2   223     3     1     2   230.2
#12       2   343     1     1     3   230.2
#13       2    34     2     2     1   242
#14       2   242     3     2     2   242
#15       2   324     1     2     3   242
#16       2    54     1     3     1   382.32
#17       2   345     3     3     2   382.32
#18       2   656     2     3     3   382.32
我有一个每个参与者一个区块的代码,但它不起作用:

nth_RT <-quantile(df$RT ~ Block * Subject, nth_RT[1])

nth\u RT我认为向量
nth\u RT
df
中的
Block
Subject
没有明确的对应关系。因此,我建议您创建一个矩阵或data.frame来清楚地显示对应关系。比如说,

grid <- expand.grid(Block = unique(df$Block), Subject = unique(df$Subject))
grid_nth_RT <- cbind(grid, nth_RT)
然后,我们可以使用for循环遍历每个
-
主题

df$nth <- array(0, nrow(df))
for(i in 1:nrow(grid_nth_RT)) {
  index <- df$Block == grid_nth_RT[i,"Block"] &
           df$Subject == grid_nth_RT[i,"Subject"]
  df$nth[index] <- quantile(df[index,"RT"], grid_nth_RT[i,"nth_RT"])
}
顺便说一下,从你的代码

quantile(df$RT ~ Block * Subject, nth_RT[1])
我想你对
~
有些误解。带有
~
的东西在R中称为
公式
。您可以查看此页面
要了解更多关于R中的
公式

非常感谢您的回答!它工作得很好。事实上,我想我绝对应该检查一些关于公式的信息。
df$nth <- array(0, nrow(df))
for(i in 1:nrow(grid_nth_RT)) {
  index <- df$Block == grid_nth_RT[i,"Block"] &
           df$Subject == grid_nth_RT[i,"Subject"]
  df$nth[index] <- quantile(df[index,"RT"], grid_nth_RT[i,"nth_RT"])
}
> df
   Subject  RT Trial Block Rank    nth
1        1 234     1     1    1 310.28
2        1 239     3     1    2 310.28
3        1 563     2     1    3 310.28
4        1 230     1     2    1 233.76
5        1 234     3     2    2 233.76
6        1 467     2     2    3 233.76
7        1 111     3     3    1 466.00
8        1 466     2     3    2 466.00
9        1 543     1     3    3 466.00
10       2  44     2     1    1 230.20
11       2 223     3     1    2 230.20
12       2 343     1     1    3 230.20
13       2  34     2     2    1 242.00
14       2 242     3     2    2 242.00
15       2 324     1     2    3 242.00
16       2  54     1     3    1 382.32
17       2 345     3     3    2 382.32
18       2 656     2     3    3 382.32
quantile(df$RT ~ Block * Subject, nth_RT[1])