bug in generating output file in R

偶尔善良 提交于 2019-12-13 05:16:41

问题


I am trying to write my output to a txt file that meets the following format requirement:

user_id \t brand_id , brand_id , brand_id \n 

"\t" and "\n" here are to insert tab space and change line. Right now I have 2 lists, user_id and brand_id and a binary matrix C, that each row corresponds to the id in user_id list in the same order, column correspond to brand_id in the same manner. If C[i,j]=1, it means I need to write out brand_id[j] on the row of user_id[i]. I wrote the following code, but it generates the output below that is incorrect. The "\" distance between user_id and brand_id is too wide for most rows. Can anyone help me correct this?

sink("result.txt")
for (i in 1:nrow(C)){
  temp <- which(C[i,]==1)
  if(length(temp)==1){
    cat(user_id[i])
    cat(" \t ")
    cat(brand_id[temp])
    cat("\n")
  }else if(length(temp)>1){
    cat(user_id[i])
    cat(" \t ")
    for (j in 1:(length(temp)-1)){
      cat(brand_id[j])
      cat(" , ")
    }
    cat(brand_id[temp[length(temp)]])
    cat("\n")
  }
}
sink()

A partial of the output from @jbaums' code and my above code was like this

8649250      28481  
887500   4571 

It seems 1 digit less in user_id creates a significant less space between user_id and brand_id. I checked the visual effect in both MAC and windows using VI and TXT editor, identical. Does this was caused by more depth bug I was not aware? Thanks


回答1:


Assuming I've understood your data and your requirements, there are simpler ways to achieve this.

By assigning the user_id vector as row names of C, and the brand_id vector as the column names, you can do something like this:

set.seed(1)    
C <- matrix(rbinom(100, 1, 0.2), nc=10, nr=10, 
            dimnames=list(LETTERS[1:10],
                          letters[1:10]))

invisible(sapply(seq_len(nrow(C)), function(i) {
  if(sum(C[i, ]) > 0) {
    cat(rownames(C)[i], '\t', 
        paste(colnames(C)[C[i, ]==1], collapse=' , '), ' \n') 
  }
}))


A    c , e , g  
B    f , h  
D    a , j  
E    d  
F    a , h  
G    a , h  
H    b  
I    c , j  
J    g , h  

Use sink either side as required.



来源:https://stackoverflow.com/questions/22571163/bug-in-generating-output-file-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!