将一个列表中的值与dataframe列匹配
我有一个数据帧和一个特征向量列表。我试图获取特征向量列表,将每个特征向量(按顺序)匹配到dataframe列,并返回一个dataframe,其中只包含特征向量大于1的列。我的伪代码如下所示:将一个列表中的值与dataframe列匹配,r,R,我有一个数据帧和一个特征向量列表。我试图获取特征向量列表,将每个特征向量(按顺序)匹配到dataframe列,并返回一个dataframe,其中只包含特征向量大于1的列。我的伪代码如下所示: 1) get eigenvalues for matrix stored as variable (called: ev) 2) iterate through each element of ev 3) check if the absolute value of the element is great
1) get eigenvalues for matrix stored as variable (called: ev)
2) iterate through each element of ev
3) check if the absolute value of the element is greater then 1
3a) if so, return dataframe column
#1
count=0
matr<as.matrix(MY_DF)
matr_temp1<-matr[,1:length(matr[,1])]#force matrix to drop COLS to have same number rows as cols
ev<-eigen(matr_temp1)$values
#2
result<-lapply(ev, function(x){
count+=1
if((abs(x)>1),return count)
})
#3
abs(x)>1
#3a
#convert numbers to df columns
df=subset(df,select=c(result))
我正在尝试这样做:
1) get eigenvalues for matrix stored as variable (called: ev)
2) iterate through each element of ev
3) check if the absolute value of the element is greater then 1
3a) if so, return dataframe column
#1
count=0
matr<as.matrix(MY_DF)
matr_temp1<-matr[,1:length(matr[,1])]#force matrix to drop COLS to have same number rows as cols
ev<-eigen(matr_temp1)$values
#2
result<-lapply(ev, function(x){
count+=1
if((abs(x)>1),return count)
})
#3
abs(x)>1
#3a
#convert numbers to df columns
df=subset(df,select=c(result))
前5个特征值:
> ev[1:5]
[1] 646127.12+ 0.00i -118038.12+ 0.00i 65537.13+ 0.00i -34741.55+33905.02i -34741.55-33905.02i
由于@user20650,我的问题的解决方案是一行
df[, abs(ev) > 1]
这在数据帧
df
上进行了迭代,只返回列表ev
中的数据大于1的列,没有示例数据帧和ev列表,帮助仅限于可能不适用于您的情况的一般想法是的,有一个。(也许你只需要df[,abs(ev)>1]
?)你得到了什么错误?正确的输出会是什么样子?@user20650,您的评论解决了我的问题,如果您将其作为答案发布,我将接受或关闭该问题