-1

I have a dataframe and a list of eigenvectors. I am trying to take the list of eigenvectors, match each one (they are in order) to the dataframe column and return a dataframe that contains only columns where the eigenvector is greater than 1. My pseudo code for it would look something like this:

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

I am trying to do that is R like this:

#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))

Obviously this needs work, Can someone suggest changes? An explanation would be awesome :)

a snippet from the dataframe that is converted to a matrix (let me know if you need more data)

    > df[1:5,1:5]
  achievement.mineWood achievement.openInventory dyad_number stat.damageDealt stat.damageTaken
1                    2                         6           1             1170             2210
2                   17                        12           1              840              800
3                   48                        37           2             2595              520
4                   16                        22           3             5410             3105
5                   36                         6           3             2720             3300

first 5 eigenvalues:

> ev[1:5]
[1]  646127.12+    0.00i -118038.12+    0.00i   65537.13+    0.00i  -34741.55+33905.02i  -34741.55-33905.02i
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • without a sample data frame and ev list, help will be limited to general ideas that may not apply to your situation – Pierre L May 23 '15 at 00:39
  • 1
    Yes , definitely would help to have a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). (maybe you just need `df[, abs(ev) > 1]` ???) – user20650 May 23 '15 at 00:41
  • What error are you getting? What will the right output look like? – Pierre L May 23 '15 at 03:12
  • 1
    @user20650, your comment solved my issue, If you will post it as answer I will accept or close the question – Rilcon42 May 23 '15 at 04:24

1 Answers1

1

The solution to my problem was a one-lines thanks to @user20650.

df[, abs(ev) > 1]

This iterated over the dataframe df and returned only columns where the data in the list ev was >1

Rilcon42
  • 9,584
  • 18
  • 83
  • 167