Pregunta

I have one vector v, created with c(), that has this data:

 v[a,b,d,z,e,f], it must be unordered

And I have a txt file with the form:

     label      1            2          3       ....
      b        100        2000          15
      z        123          14          12
      a         55         565          55
     .....

I have extracted the txt file, that is delimited with tabs with strplit

      ext_data<-strsplit(file,"\t") 

What I want to do is to see if the elements of vector V matches one of the elements of label, it can be there like not, and after that extract the corresponding elements of the column 1 of the txt file, then the elements of column 2 and so on

I have made the matching using for loop, but is taking too much time, because the txt file contains too much data, like this (algorithmically)

      for i=1 to length(v)
             for pos=2 to ext_data      #I put pos=2 because I start in the second row
                  if match(vector) and ext_data(pos,1)  
                       retrieve data from column C     

Any suggestion?

In rough way what I want is to know if there could be a way to use the match, but with columns, maybe transforming the column label in a row?

¿Fue útil?

Solución

Just creating some test data to illustrate my solution:

testdata <- data.frame(namecol=c("b","r","a","j","z","l","s","n","t"),
                       v1=sample(1:1000,9),
                       v2=sample(1:1000,9),
                       v3=sample(1:1000,9))
vecfind <- c("a","b","d","z","e","f")

Using [[]] or $, you can select the first element of a data frame as a vector, and then using the which and %in% functions, you can get the numeric row indices and then extract the elements, like this:

v1_elements <- testdata[which(testdata[[1]] %in% vecfind),2]
v2_elements <- testdata[which(testdata[[1]] %in% vecfind),3]
v3_elements <- testdata[which(testdata[[1]] %in% vecfind),4]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top