R语言:将函数每次调用结果存入指定名称向量的问题
嘿,作为R语言新手能自己动手写函数处理区间统计,已经超棒啦!针对你想把每次调用Window_length的结果存入特定名称向量的需求,我给你几个实用的实现方案,还有小技巧帮你简化代码:
首先先补全你的函数,让它能返回完整的区间统计结果(假设你要覆盖到正数区间):
Window_length <- function(x) { first_interval <- length(which(x <= -1.5)) second_interval <- length(which(x <= -1 & x > -1.5 )) third_interval <- length(which(x <= -0.5 & x > -1 )) fourth_interval <- length(which(x <= 0 & x > -0.5 )) fifth_interval <- length(which(x <= 0.5 & x > 0 )) sixth_interval <- length(which(x <= 1 & x > 0.5 )) seventh_interval <- length(which(x <= 1.5 & x > 1 )) eighth_interval <- length(which(x > 1.5 )) # 给结果向量命名,方便后续查看 result <- c( "<=-1.5" = first_interval, "-1.5~-1" = second_interval, "-1~-0.5" = third_interval, "-0.5~0" = fourth_interval, "0~0.5" = fifth_interval, "0.5~1" = sixth_interval, "1~1.5" = seventh_interval, ">1.5" = eighth_interval ) return(result) }
方案1:全局赋值到指定向量(快速但需谨慎)
如果你希望函数自动把结果追加到某个特定向量,可以用<<-操作符修改全局环境中的变量。不过要注意:这种方式会直接修改全局变量,新手容易不小心覆盖其他数据,所以使用时要确认变量名称唯一。
# 先在全局环境创建空的目标向量 my_specific_vector <- c() # 修改函数,添加全局赋值逻辑 Window_length <- function(x) { # 区间计算代码同上... result <- c( "<=-1.5" = length(which(x <= -1.5)), "-1.5~-1" = length(which(x <= -1 & x > -1.5 )), "-1~-0.5" = length(which(x <= -0.5 & x > -1 )), "-0.5~0" = length(which(x <= 0 & x > -0.5 )), "0~0.5" = length(which(x <= 0.5 & x > 0 )), "0.5~1" = length(which(x <= 1 & x > 0.5 )), "1~1.5" = length(which(x <= 1.5 & x > 1 )), ">1.5" = length(which(x > 1.5 )) ) # 将结果追加到全局的特定向量 my_specific_vector <<- c(my_specific_vector, result) return(result) # 可选,返回结果方便即时查看 } # 调用示例 Window_length(c(-2, -1.2, 0, 1.6)) Window_length(c(-0.8, 0.3, 1.2)) # 查看最终的特定向量 my_specific_vector
方案2:手动追加结果(更安全易控)
如果不想修改全局变量,推荐让函数返回结果,然后手动把结果追加到目标向量。这种方式更清晰,也不容易出错。
# 提前创建空的目标向量 my_specific_vector <- c() # 用之前返回结果的函数版本 Window_length <- function(x) { result <- c( "<=-1.5" = length(which(x <= -1.5)), "-1.5~-1" = length(which(x <= -1 & x > -1.5 )), "-1~-0.5" = length(which(x <= -0.5 & x > -1 )), "-0.5~0" = length(which(x <= 0 & x > -0.5 )), "0~0.5" = length(which(x <= 0.5 & x > 0 )), "0.5~1" = length(which(x <= 1 & x > 0.5 )), "1~1.5" = length(which(x <= 1.5 & x > 1 )), ">1.5" = length(which(x > 1.5 )) ) return(result) } # 每次调用函数后,手动追加结果 my_specific_vector <- c(my_specific_vector, Window_length(c(-2, -1.2, 0, 1.6))) my_specific_vector <- c(my_specific_vector, Window_length(c(-0.8, 0.3, 1.2))) # 查看结果 my_specific_vector
方案3:用列表存储(适合保留每次调用的完整结构)
如果每次调用函数返回的是多个数值(比如8个区间长度),用向量存储会把所有值扁平化,难以区分哪次调用对应哪些结果。这时用列表存储会更合适,能保留每次调用的完整结构:
# 提前创建空列表 my_results_list <- list() # 调用函数并加入列表,可以用索引或命名区分 my_results_list[[1]] <- Window_length(c(-2, -1.2, 0, 1.6)) my_results_list[["second_call"]] <- Window_length(c(-0.8, 0.3, 1.2)) # 查看列表中的结果 my_results_list
额外小技巧:用cut()简化区间计算
你的区间统计逻辑可以用R内置的cut()函数来简化,代码更简洁,也不容易出错:
Window_length_simpler <- function(x) { # 定义区间断点 breaks <- c(-Inf, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, Inf) # 把x分到对应的区间,include.lowest确保左端点被包含 interval_groups <- cut(x, breaks = breaks, include.lowest = TRUE) # 统计每个区间的数量,转成向量返回并命名 result <- as.vector(table(interval_groups)) names(result) <- c("<=-1.5", "-1.5~-1", "-1~-0.5", "-0.5~0", "0~0.5", "0.5~1", "1~1.5", ">1.5") return(result) } # 测试效果 Window_length_simpler(c(-2, -1.2, 0, 1.6))
内容的提问来源于stack exchange,提问作者Nono_sad
相关产品推荐
相关产品推荐

