0

I thought many times before posting this, it is really simple but I couldn't find a way to do that, sorry if it is very basic.

I am trying to find a simple command to output a dataframe that includes 2 columns, the value resulting from a for loop, along with the variable name in that loop, here's the command I am using

for (i in 1:length(x)) {

          k <- i*5

         ##### saving the values that comes out of the loop

    if (exists("v")== "TRUE" ) {
    v= append(v,k)
    }
    if (exists("v")== "FALSE"){
    v <- k
    }

         ###### adding the name of the column

      n <- names(x[i])


    if (exists("m")== "TRUE" ) {
    m= append(m,n)
    }
    if (exists("m")== "FALSE"){
    m <- n
    }
}
         ###### I manually put them into a dataframe and bind them

         v <- data.frame(v)
         m <- data.frame(m)

         df <- cbind(v,m)

the output data frame I am looking for:

       v           m
       5         MUYC
       10        KJIO
       15        KLGJ

I know this is retarded, the command actually works!! I am sure there are simpler ways to write this! I just couldn't find ones so far.

here's a simple reproducible example

the x matrix

structure(list(MUYC = 1555L, KJIO = 320L, KLGJ = 5132L), .Names = c("MUYC", 
"KJIO", "KLGJ"), class = "data.frame", row.names = c(NA, -1L))

Many thanks,

Error404
  • 6,959
  • 16
  • 45
  • 58
  • 1
    Please make your code [reproducible](http://stackoverflow.com/a/5963610/1412059) and edit an actual question into your post. – Roland Jul 23 '13 at 09:44
  • What are x, v, m, n at the beginning? – shadow Jul 23 '13 at 09:49
  • Prepopulate your data.frame, then fill values into it using `[row,column]` indices, or use something from the `*apply` family instead of `for`. – Thomas Jul 23 '13 at 09:51
  • @Roland, I is this reproducible? – Error404 Jul 23 '13 at 09:55
  • @shadow, x is the matrix I have, v,m and n are defined in the above program. – Error404 Jul 23 '13 at 09:56
  • 2
    Please fix the syntax errors. It seems like you want `cbind.data.frame(v=seq_along(names(x))*5, m=names(x))` which indeed seems a bit silly. Also, you don't habe a `matrix`. It's a `data.frame`. – Roland Jul 23 '13 at 10:00
  • The first line of your for-loop isn't valid syntax. – Thomas Jul 23 '13 at 10:04
  • Ok, now the loop is working fine – Error404 Jul 23 '13 at 10:14
  • @Roland yes your command does the same, but why do you consider it silly? – Error404 Jul 23 '13 at 10:17
  • @Error404 Probably because I don't know the context. – Roland Jul 23 '13 at 10:19
  • 2
    @Error404 here is a tip for next time - a question that takes 10 comments and 3 edits to become barely understandable will not attract many good answers. Focus on making a toy [**reproducible example**](http://stackoverflow.com/q/5963269/1478381) that illustrates the crux of the problem without unnecessary detail that makes it harder to understand the problem (i.e. it doesn't have to be your actual code - make a simple example up instead). – Simon O'Hanlon Jul 23 '13 at 10:49
  • @SimonO101, Thanks for your kind comment. This is exactly what I did above, sorry for the multiple edits. My command is doing alright, I was just thinking of a more professional, reliable way to write this. The problem here is simply that it is taking me a half page of command to save an output of a 2-line for loop. But seems like I am going to stick the command I have above! – Error404 Jul 23 '13 at 10:58

1 Answers1

3

To my mind it seems all your want to do is this:

data.frame( v = seq( from = 5 , by = 5 , length.out = length(x) ) , m = names(x) )
#   v    m
#1  5 MUYC
#2 10 KJIO
#3 15 KLGJ

But I don't know how generalisable to your problem this will be.

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184