R 移动向量
我有一个数据框,我想“对齐”每一列,使每一列的最大值在同一行上 我试图使用基本功能来实现这一点,但我得到了错误的结果,即只是覆盖而不是移动。我刚刚在Hmisc中找到了滞后函数,但是,我确信在base中有一种方法可以做到这一点,我只是想它是错误的。我更喜欢这样,因为当我稍后尝试在另一台具有不同verison of R的计算机上运行此程序时,总会有一些软件包不受支持 谢谢你的帮助R 移动向量,r,R,我有一个数据框,我想“对齐”每一列,使每一列的最大值在同一行上 我试图使用基本功能来实现这一点,但我得到了错误的结果,即只是覆盖而不是移动。我刚刚在Hmisc中找到了滞后函数,但是,我确信在base中有一种方法可以做到这一点,我只是想它是错误的。我更喜欢这样,因为当我稍后尝试在另一台具有不同verison of R的计算机上运行此程序时,总会有一些软件包不受支持 谢谢你的帮助 maxIndices<-apply(df,2,function(x){ maxInt<-max(x,na.r
maxIndices<-apply(df,2,function(x){
maxInt<-max(x,na.rm=T)
maxInt_indx<-which(x==maxInt)
})
maxMaxIndex<-max(maxIndices)
minMaxIndex<-min(maxIndices)
##
apply(df,2,function(x){
maxInt<-max(x,na.rm=T)
maxInt_indx<-which(x==maxInt)
shift<-maxMaxIndex-maxInt_indx
shifted_vec<-c(rep(NA,times=shift), x[1:length(x)+shift]) ## this is producing the wrong results
# shifted_vec<-Lag(x,shift) # is there a way to do this using just base functionality
})
我想你只是在一行中输入了一个错误:
shifted_vec<-c(rep(NA,times=shift), x[1:(length(x)-shift)]) ## this is producing the wrong results
我想你只是在一行中输入了一个错误:
shifted_vec<-c(rep(NA,times=shift), x[1:(length(x)-shift)]) ## this is producing the wrong results
我对移位函数实现可能/应该是什么样子的解释:
#' function that shifts vector values to right or left
#'
#' @param x Vector for which to shift values
#' @param n Number of places to be shifted.
#' Positive numbers will shift to the right by default.
#' Negative numbers will shift to the left by default.
#' The direction can be inverted by the invert parameter.
#' @param invert Whether or not the default shift directions
#' should be inverted.
#' @param default The value that should be inserted by default.
shift <- function(x, n, invert=FALSE, default=NA){
stopifnot(length(x)>=n)
if(n==0){
return(x)
}
n <- ifelse(invert, n*(-1), n)
if(n<0){
n <- abs(n)
forward=FALSE
}else{
forward=TRUE
}
if(forward){
return(c(rep(default, n), x[seq_len(length(x)-n)]))
}
if(!forward){
return(c(x[seq_len(length(x)-n)+n], rep(default, n)))
}
}
我对移位函数实现可能/应该是什么样子的解释:
#' function that shifts vector values to right or left
#'
#' @param x Vector for which to shift values
#' @param n Number of places to be shifted.
#' Positive numbers will shift to the right by default.
#' Negative numbers will shift to the left by default.
#' The direction can be inverted by the invert parameter.
#' @param invert Whether or not the default shift directions
#' should be inverted.
#' @param default The value that should be inserted by default.
shift <- function(x, n, invert=FALSE, default=NA){
stopifnot(length(x)>=n)
if(n==0){
return(x)
}
n <- ifelse(invert, n*(-1), n)
if(n<0){
n <- abs(n)
forward=FALSE
}else{
forward=TRUE
}
if(forward){
return(c(rep(default, n), x[seq_len(length(x)-n)]))
}
if(!forward){
return(c(x[seq_len(length(x)-n)+n], rep(default, n)))
}
}
它应该是一行,但为了确保你的答案是正确的,你能发布一些示例数据并输出吗?@初学者你的代码不是这样工作的;试试看,你是对的。我又一次把dplyr::lag和stats::lagIt搞混了,应该是一行,但为了确保你的答案是正确的,你能发布一些示例数据和输出吗?@初学者你的代码不是这样工作的;试试看,你是对的。我再次混淆了dplyr::lag和stats::lagbase::lag返回的是一个时间序列对象,这不是我在这里想要的。base::lag返回的是一个时间序列对象,这不是我在这里想要的。