使用tidyverse(dplyr和purrr)更换两个for回路

使用tidyverse(dplyr和purrr)更换两个for回路,r,for-loop,dplyr,tidyverse,purrr,R,For Loop,Dplyr,Tidyverse,Purrr,我有一张桌子 raw.tb #> # A tibble: 10 x 4 #> geno ind X Y #> * <fctr> <fctr> <int> <int> #> 1 san1w16 A1 467 383 #> 2 san1w16 A1 465 378 #> 3 san1w16 B1 464 378 #>

我有一张桌子

raw.tb
#> # A tibble: 10 x 4
#>       geno    ind     X     Y
#>  *  <fctr> <fctr> <int> <int>
#>  1 san1w16     A1   467   383
#>  2 san1w16     A1   465   378
#>  3 san1w16     B1   464   378
#>  4 san1w16     B1   464   377
#>  5 san2w16     A1   464   376
#>  6 san2w16     A1   464   375
#>  7 san2w16     B1   463   375
#>  8 san2w16     B1   463   374
#>  9 san3w16     A1   463   373
#> 10 san3w16     A1   463   372

我猜这可以使用tidyverse来完成,但是我已经研究过使用group_by with do和nest with map,我无法让它工作

我有些猜测,因为我不清楚你到底想做什么

raw.tb <- structure(list(geno = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "san1w16", class = "factor"), ind = structure(c(1L, 
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 1L), .Label = c("A1", "B1", "C1", 
"D1", "E1"), class = "factor"), X = c(467L, 465L, 464L, 464L, 
464L, 464L, 463L, 463L, 463L, 463L), Y = c(383L, 378L, 378L, 
377L, 376L, 375L, 375L, 374L, 373L, 372L)), .Names = c("geno", 
"ind", "X", "Y"), row.names = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10"), class = c("tbl_df", "tbl", "data.frame"
)) %>% as_tibble(); raw.tb 
#> # A tibble: 10 x 4
#>       geno    ind     X     Y
#>  *  <fctr> <fctr> <int> <int>
#>  1 san1w16     A1   467   383
#>  2 san1w16     A1   465   378
#>  3 san1w16     B1   464   378
#>  4 san1w16     B1   464   377
#>  5 san1w16     C1   464   376
#>  6 san1w16     C1   464   375
#>  7 san1w16     D1   463   375
#>  8 san1w16     D1   463   374
#>  9 san1w16     E1   463   373
#> 10 san1w16     A1   463   372
或许

raw.tb %>% group_by(geno) %>% gather(XY, Value, -geno, -ind) %>%
           arrange(geno, ind) %>% group_by(ind, geno)  %>%
           summarise(Value = mean(Value))
#> # A tibble: 5 x 3
#> # Groups:   ind [?]
#>      ind    geno    Value
#>   <fctr>  <fctr>    <dbl>
#> 1     A1 san1w16 421.3333
#> 2     B1 san1w16 420.7500
#> 3     C1 san1w16 419.7500
#> 4     D1 san1w16 418.7500
#> 5     E1 san1w16 418.0000

我找到了。希望我的回答能让事情更清楚。我向@Eric表示歉意,因为我没有说得更清楚

基本上,我写了一个函数,给定一个x,y坐标矩阵,使用第一个和最后一个点作为基线旋转坐标。我没有详细说明该函数,因为它很长,不是这里的重点,但基本上,该函数属于以下类型:

rotate.coord <- function(mat){
  for(i in 1:dim(mat)[1]{
    x1=(dim(coord.rot)[1])
    x2=1
    .
    .
    (theta is computed based on x1 and x2)
    .
    .
    xn=mat[z,1]*cos(theta)+mat[z,2]*sin(theta)
    yn=-mat[z,1]*sin(theta)+mat[z,2]*cos(theta)
    mat[z,1]=xn
    mat[z,2]=yn
    }
    mat = as_tibble(mat)
    return(mat)
}
具有:

raw.tb
#> # A tibble: 10 x 4
#>       geno    ind     X     Y
#>  *  <fctr> <fctr> <int> <int>
#>  1 san1w16     A1   467   383
#>  2 san1w16     A1   465   378
#>  3 san1w16     B1   464   378
#>  4 san1w16     B1   464   377
#>  5 san2w16     A1   464   376
#>  6 san2w16     A1   464   375
#>  7 san2w16     B1   463   375
#>  8 san2w16     B1   463   374
#>  9 san3w16     A1   463   373
#> 10 san3w16     A1   463   372
我想做

raw.nt <- raw.tb %>% 
 group_by(geno,ind) %>% 
 nest()

raw.nt2 <- raw.nt %>% 
  mutate(rot = map(data,rotate.coo))

这将创建一个新的嵌套TIBLE,其中每个组的原始.nt2$rot是来自每个原始.nt$data组的旋转矩阵

看起来您需要一个按Summary分组您的意思是使用dplyr::Summary?您的循环不起作用?你能发布你想要的结果吗?是的right@EricFail我刚刚尝试了这个循环,它工作了:forg in uniqueraw.tb$geno{fori in uniqueraw.tb[raw.tb$geno==g,]$ind{raw.tb[raw.tb$geno==g&raw.tb$ind==I,c3,4]=t raw.tb[raw.tb$geno==g&raw.tb$ind==I,c3,4]}@akrun,如果我理解Summary的功能,那么它的问题在于,它会为每个组返回一个值,而我希望返回一个与我的组矩阵大小相同的矩阵。我几乎是在寻找等价的tidyverse:'toto=as.listsplitraw.tb[,c3,4],listraw.tb$ind,raw.tb$geno toto2=LapplyTo,some.function.for.a.matrix’你能不能在你的问题中包括这个,包括输出?我想确保我们得到相同的输出。
rotate.coord <- function(mat){
  for(i in 1:dim(mat)[1]{
    x1=(dim(coord.rot)[1])
    x2=1
    .
    .
    (theta is computed based on x1 and x2)
    .
    .
    xn=mat[z,1]*cos(theta)+mat[z,2]*sin(theta)
    yn=-mat[z,1]*sin(theta)+mat[z,2]*cos(theta)
    mat[z,1]=xn
    mat[z,2]=yn
    }
    mat = as_tibble(mat)
    return(mat)
}
raw.tb
#> # A tibble: 10 x 4
#>       geno    ind     X     Y
#>  *  <fctr> <fctr> <int> <int>
#>  1 san1w16     A1   467   383
#>  2 san1w16     A1   465   378
#>  3 san1w16     B1   464   378
#>  4 san1w16     B1   464   377
#>  5 san2w16     A1   464   376
#>  6 san2w16     A1   464   375
#>  7 san2w16     B1   463   375
#>  8 san2w16     B1   463   374
#>  9 san3w16     A1   463   373
#> 10 san3w16     A1   463   372
raw.nt <- raw.tb %>% 
 group_by(geno,ind) %>% 
 nest()

raw.nt2 <- raw.nt %>% 
  mutate(rot = map(data,rotate.coo))