本文介绍了R输出到txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2列的数据框

userID itemID
1       101
1       103
1       107
2       102
2       103
3       104
...

我想要的输出是将文件写入result.txt
1 \ t 101,103,107
2 \ t 102,103
3 \ t 104
\ t表示用户ID和itemID之间的制表符距离.这不像表格那样对齐.我更多地是Java和Python的背景知识,R中用于一般用途的较低级别的编写命令是什么?

The output I want is to write a file to result.txt
1 \t 101 , 103 , 107
2 \t 102 , 103
3 \t 104
here \t means a tab distance between userID and the itemID. This is not as aligned as a table. I am more of Java and Python background, what are the lower level writing commands in R for general purpose?

推荐答案

您可以为此使用dplyr包

you can use dplyr package for this

library(dplyr)

df.summary <- df %.%
  group_by(userId) %.%
  summarise(itemId = paste(itemId, collapse = ","))


write.table(x=df.summary,file='new_file.tsv',sep='\t',row.names=F)

这篇关于R输出到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 07:43