在R中生成输出文件的错误 [英] bug in generating output file in R
问题描述
我正在尝试将输出写入符合以下格式要求的txt文件:
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"和"\ n"用于插入制表符空间并更改行.
现在我有2
列表,user_id
和brand_id
和一个binary matrix C
,每一行以相同的顺序对应于user_id
列表中的id
,列对应于相同的brand_id
方式. If C[i,j]=1
,这意味着我需要在user_id[i]
的行上写出brand_id[j]
.
我编写了以下代码,但是它在下面生成了不正确的输出.对于大多数行,user_id
和brand_id
之间的"\"
距离太宽.谁能帮我纠正这个问题?
"\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()
@jbaums代码的一部分输出,而我上面的代码就是这样
A partial of the output from @jbaums' code and my above code was like this
8649250 28481
887500 4571
在user_id中似乎少了1位数,从而在user_id和brand_id之间产生了明显更少的空间.我使用相同的VI和TXT编辑器检查了MAC和Windows中的视觉效果.这是由我不知道的更多深度错误引起的吗?谢谢
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
推荐答案
假设我已经了解了您的数据和您的要求,可以通过更简单的方法来实现.
Assuming I've understood your data and your requirements, there are simpler ways to achieve this.
通过将user_id
向量指定为C
的行名,并将brand_id
向量指定为列名,您可以执行以下操作:
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
根据需要在任一侧使用sink
.
Use sink
either side as required.
这篇关于在R中生成输出文件的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!