我试图计算两个向量之间的夹角,其中两个向量都在同一数据帧中,但是每个数据帧在一个列表中。这是我尝试过的MRE。
rm(list=ls())
coords <- rnorm(10)
x.h <- rnorm(10)
y.h <- rnorm(10)
liklihood.h <- rnorm(10)
x.MB <- rnorm(10)
y.MB <- rnorm(10)
x.a <- rnorm(10)
y.a <- rnorm(10)
day1 <- data.frame(coords,x.h,y.h,liklihood.h,x.MB,y.MB,x.a,y.a)
coords <- rnorm(10)
x.h <- rnorm(10)
y.h <- rnorm(10)
liklihoo.d.h <- rnorm(10)
x.MB <- rnorm(10)
y.MB <- rnorm(10)
x.a <- rnorm(10)
y.a <- rnorm(10)
day2 <- data.frame(coords,x.h,y.h,liklihood.h,x.MB,y.MB,x.a,y.a)
data <- list(day1,day2)
angle <- function(x,y){
dot.prod <- x%*%y
norm.x <- norm(x,type="2")
norm.y <- norm(y,type="2")
theta <- acos(dot.prod / (norm.x * norm.y))
as.numeric(theta)
}
append_angles <- function(df1){
vect1 = c((df1$x.h-df1$x.MB),(df1$y.h-df1$y.MB))
vect2 = c((df1$x.a-df1$x.MB),(df1$y.a-df1$y.MB))
df1$theta = (angle(vect1, vect2)*(180/pi))
return(df1)
}
data <- mapply(append_angles, data, SIMPLIFY = FALSE)
我最终从针对每个数据帧重复的计算中而不是针对每一行的计算中获得第一个值。我如何使它计算出每一行的值?
答案 0 :(得分:2)
基于创建的函数,我们可以split
逐行然后应用
out <- Map(function(x) do.call(rbind, lapply(split(x, seq_len(nrow(x))),
append_angles)), data)
out
#[[1]]
# coords x.h y.h liklihood.h x.MB y.MB x.a y.a theta
#1 -0.545880758 -1.3169081 -0.07430856 -0.03373792 0.06735770 0.5266526 1.4520752 0.14379993 141.077262
#2 0.536585304 0.5982691 -0.60515695 -0.58542756 0.01710596 -1.0736261 0.4082015 -0.88610999 13.255879
#3 0.419623149 -0.7622144 -1.70964518 0.61285136 -0.34365937 0.7696819 -0.4403340 -0.02557419 2.651103
#4 -0.583627199 -1.4290903 -0.26869311 1.51712249 -0.66789220 1.7709054 1.3185662 -0.32773539 63.893042
#5 0.847460017 0.3322444 -0.64859151 0.65738044 -0.25574457 -0.1961822 0.5713866 -0.07596102 45.845198
#6 0.266021979 -0.4690607 -0.09411013 -1.07418134 -0.46120796 0.2047497 -1.2799872 -1.35466363 26.196849
#7 0.444585270 -0.3349868 -0.08554095 -4.46956441 1.47164158 -0.5965981 -1.2388796 -0.96080882 23.448044
#8 -0.466495124 1.5362522 0.11953107 0.36904502 -0.09196032 1.1782477 -0.9225911 0.22495434 98.033303
#9 -0.848370044 0.6099945 -0.11629639 0.16922669 0.33519430 1.0494212 1.3496121 -0.12316046 27.599124
#10 0.002311942 0.5163357 -0.94382724 -1.82219032 -0.23186459 0.5609812 -1.5676166 0.00104102 93.693684
#[[2]]
# coords x.h y.h liklihood.h x.MB y.MB x.a y.a theta
#1 -0.571243248 -0.2327672 0.08314367 -0.03373792 1.01283626 -2.46745604 0.667163872 -0.07045269 17.822834
#2 0.422621775 -1.4203631 2.78494703 -0.58542756 0.02534344 -0.05040743 -0.008576239 -0.75031916 150.209075
#3 -1.673385810 -0.8133227 0.59528232 0.61285136 -0.50715002 0.24643818 0.092440453 -0.09401376 160.860935
#4 -1.136025931 0.4610693 0.07766635 1.51712249 -0.50694324 1.53136287 0.320282642 0.41274946 2.823761
#5 0.361212489 -0.2148936 -0.55869661 0.65738044 0.26498877 0.59163787 0.165091251 -1.25747896 19.551954
#6 1.056864213 0.1389452 -0.80676768 -1.07418134 0.59168174 -1.61854195 -1.053206734 0.19706349 13.026765
#7 -1.992920453 0.8442304 -0.51070906 -4.46956441 -0.28834071 -0.63493218 0.447394677 -1.89231492 65.926033
#8 0.764385054 -1.3084503 -0.55648712 0.36904502 -0.52907038 1.35675449 1.057136825 0.25217616 77.312082
#9 0.007165349 1.0451687 0.24112516 0.16922669 -0.17986538 -0.75802712 -1.475157330 -0.64785092 135.937090
#10 0.101974336 1.2101739 -0.57533702 -1.82219032 -0.93043770 0.37536404 0.200732860 -0.26605768 5.607831