r语言 - for循环中要替换的项数不是替换长度的倍数

  • 本文关键字:替换 r语言 循环 for r loops for-loop
  • 更新时间 :
  • 英文 :


我正在尝试运行for循环代码来计算加权平均值。我的问题是,一旦我考虑到年龄不确定性,lsp中的一些最小和最大年龄超出了我的dynamics数据帧的时间序列,从而导致错误Error in for loop number of items to replace is not a multiple of replacement length

这意味着它只是不计算该数据子集的加权平均值,并且有些行只是留空。是否有一种方法可以告诉R仍然使用lspdynamics中发现的年份来计算它?也因为某种原因,它跳过了第19个子集直接跳到第20个?

多谢


## read in the datasets
lsp <- read_csv("lsp_IP.csv")
dynamics <- read_csv("dynamicsLSP.csv")
# create two new variables
dynamics$subset <- NA # will store the subset label
dynamics$y <- NA # will store y for each subset
for(j in 1:nrow(lsp)) ## loop over every row of lsp
{

x_temp=seq(lsp$min[j],lsp$max[j]) # temporary x
y_temp=dnorm(x_temp,mean=lsp$mean[j],sd=lsp$sd[j]) # temporary y

## the index will give the locations in the dynamics dataset that are between min and max for jth index point
index <- which((dynamics$Year >= lsp$min[j] & dynamics$Year <= lsp$max[j]) == TRUE)

dynamics$subset[index] <- j # add index point subset label to dynamics data
dynamics$y[index] <- y_temp # add y for that group to dynamics data
} # end j loop
## Note there's warnings because dynamics$Year doesn't go back further than 1815 whereas the lsp min years goes to 1768 
view(dynamics)

## get weighted average for each subset
calc<-dynamics %>% group_by(subset) %>% summarise(weighted_average = sum(y*dynamics)) 
### LSP
structure(list(Depth = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 
8.5, 10.5, 13.5, 14.5, 18.5, 19.5, 27.5, 28.5, 32, 35.5, 40.5, 
41.5), RSL = c(0.03, 0.03, 0.01, 0.01, -0.04, -0.01, -0.03, 0, 
0.04, 0.03, 0, -0.01, -0.05, -0.07, -0.19, -0.24, -0.31, -0.31, 
-0.27, -0.29), RSL_err_1sig = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1), mean = c(2001.754499, 1994.278776, 1987.678949, 1980.805889, 
1973.270485, 1965.018421, 1957.442729, 1952.134369, 1949.031929, 
1945.148184, 1939.132213, 1936.957531, 1927.311071, 1924.379033, 
1897.26123, 1892.977317, 1876.1995, 1858.135589, 1825.967544, 
1820.605298), sd = c(1.000452154, 1.833424335, 2.039071018, 2.508755788, 
2.276872259, 1.359820545, 1.989239417, 1.810064954, 1.454098006, 
1.86095396, 3.035939354, 3.380096574, 4.216567862, 4.427207081, 
4.386723491, 4.6104428, 8.24146479, 9.241324604, 8.193859739, 
10.61608635), min = c(1998.753143, 1988.778503, 1981.561736, 
1973.279622, 1966.439868, 1960.938959, 1951.475011, 1946.704174, 
1944.669635, 1939.565322, 1930.024395, 1926.817241, 1914.661367, 
1911.097412, 1884.10106, 1879.145989, 1851.475106, 1830.411615, 
1801.385965, 1788.757039), max = c(2004.755855, 1999.779049, 
1993.796162, 1988.332156, 1980.101102, 1969.097883, 1963.410447, 
1957.564564, 1953.394223, 1950.731046, 1948.240031, 1947.097821, 
1939.960775, 1937.660654, 1910.4214, 1906.808645, 1900.923894, 
1885.859563, 1850.549123, 1852.453557), length = c(7.002712924, 
12.00054601, 13.23442611, 16.05253473, 14.66123355, 9.15892327, 
12.9354365, 11.86038972, 9.724588036, 12.16572376, 19.21563612, 
21.28057944, 26.29940717, 27.56324249, 27.32034095, 28.6626568, 
50.44878874, 56.44794762, 50.16315843, 64.6965181)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
cols = list(Depth = structure(list(), class = c("collector_double", 
"collector")), RSL = structure(list(), class = c("collector_double", 
"collector")), RSL_err_1sig = structure(list(), class = c("collector_double", 
"collector")), mean = structure(list(), class = c("collector_double", 
"collector")), sd = structure(list(), class = c("collector_double", 
"collector")), min = structure(list(), class = c("collector_double", 
"collector")), max = structure(list(), class = c("collector_double", 
"collector")), length = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"))
## Dynamics (only head because time series is ~200 years!)

structure(list(Year = c(1900, 1901, 1902, 1903, 1904, 1905), 
dynamics = c(-39.10076505, -55.24936121, -77.49252244, -67.85424774, 
-42.96636324, -78.813866)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

我们可以使用fuzzyjoin

library(dplyr)
library(fuzzyjoin)
lsp1 <- lsp %>% 
rowwise %>% 
mutate(x_temp = list(min:max), y_temp = dnorm(x_temp[[1]],
mean = mean, sd = sd)) %>%
ungroup

fuzzy_inner_join(lsp1, dynamics, by = c("min" = "Year", "max" = "Year"), 
match_fun = list(`<=`, `>=`)) %>% 
select(names(dynamics), y_temp)

最新更新