问题
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