0

I have data frame "x" like this :

        meme   webId  timeStamp
        2501   68814  281322.1 
        2501    2679  305813.0
        2501     948  306025.6 

I want to use "meme" and "webId" as row and column names and timeStamp as element in "mat" data frame. I wrote this:

cols<-unique(x[,"webId"])

rows<-unique(x[,"meme"])

mat<-data.frame(matrix(data=9999999,nrow=length(rows),ncol=length(cols)))

colnames(mat)<-c(cols)

rownames(mat)<-c(rows)

for(i in 1:length(x))
        mat[rownames(mat)==x[i,"meme"],colnames(mat)==x[i,"webId"]]<-x[i,"timeStamp"]

but nothing changed. what is the problem? please help me!!!

8
  • You don't need a loop. Try mat[match(rownames(mat), x$meme), match(colnames(mat), x$webId)] <- x$timeStamp
    – akrun
    May 24, 2015 at 13:03
  • @akrun the error is something like this: Error in [<-.data.frame(*tmp*, match(rownames(mat), x$meme), match(colnames(mat), : new columns would leave holes after existing columns May 24, 2015 at 13:07
  • Is it based on the same example you posted? It is not giving me any errors
    – akrun
    May 24, 2015 at 13:08
  • 1
    @ZahraAminolroaya Using your code, the output of "mat" looks correct. Do you want x to change?
    – Nadine
    May 24, 2015 at 13:29
  • 2
    @ZahraAminolroaya Please update the new data in your post. It is not easy to understand the data from the comments due to the formatting. Also, if you can use dput to show the data, it would be great
    – akrun
    May 24, 2015 at 13:38

2 Answers 2

0

In the for loop, it seems that you mean to iterate over all the rows in x, and fill all the values into mat one by one. Instead you only iterate over 3 rows. length(x) gives the number of columns not the number of rows. This is the correct code for iterating over all rows:

for(i in 1:nrow(x))
        mat[rownames(mat)==x[i,"meme"],colnames(mat)==x[i,"webId"]]<-x[i,"timeStamp"]

I suspect that the x dataframe contains more values than the ones you posted. In your example, the number of rows equals the number of columns, that's why the commenters couldn't find a problem with it. The problem is not evident in your example.

0
0

You could get the 'row/column' index by using match, cbind it and assign the 'timeStamp' elements to the positions specified by the index in 'mat'.

 mat[cbind(match(x$meme, rownames(mat)),
             match(x$webId, colnames(mat)))] <- x$timeStamp

 mat
 #     428 2679 68814 948
 #2505  13   11     8   3
 #2510  16    6    14   1
 #2501   7    4     5  10
 #2508  12    2     9  15

Checking with the results from the for loop

 for(i in 1:nrow(x))
    mat1[rownames(mat1)==x[i,"meme"],
             colnames(mat1)==x[i,"webId"]]<-x[i,"timeStamp"]

 mat1
 #     428 2679 68814 948
 #2505  13   11     8   3
 #2510  16    6    14   1
 #2501   7    4     5  10
 #2508  12    2     9  15

Benchmarks

set.seed(21)
x1 <- data.frame(meme= rep(sample(1000), each=200), 
   webId= rep(sample(35000, 200, replace=FALSE), 1000), 
      timeStamp=rnorm(1000*200))
set.seed(324)
mat2 <- matrix(, 1000, 200, 
    dimnames=list(sample(unique(x1$meme)),sample(unique(x1$webId))))
mat3 <- mat2

system.time({
  mat2[cbind(match(x1$meme, rownames(mat2)),
         match(x1$webId, colnames(mat2)))] <- x1$timeStamp
     })
 # user  system elapsed 
 #  0.181   0.001   0.181 

system.time({

 for(i in 1:nrow(x1))
    mat3[rownames(mat3)==x1[i,"meme"],
            colnames(mat3)==x1[i,"webId"]]<-x1[i,"timeStamp"]

 })
# user  system elapsed 
#172.588  10.445 183.062 

 identical(mat2, mat3)
 #[1] TRUE

data

set.seed(24)
x <- data.frame(meme=rep(c(2501, 2505, 2508, 2510), each=4),
    webId= rep(c(68814, 2679, 948, 428), 4), timeStamp= sample(16))
set.seed(33)
mat <- matrix(, 4, 4, dimnames=list(sample(unique(x$meme)),
    sample(unique(x$webId))))
mat1 <- mat
1
  • @ZahraAminolroaya Added some benchmarks. The for loop in the current state is very slow compared to the match. It may be possible to improve the timings in for loop. But, I think the vectorized approach would be better here.
    – akrun
    May 24, 2015 at 14:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.