Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R输出到txt文件_R_Output - Fatal编程技术网

R输出到txt文件

R输出到txt文件,r,output,R,Output,我有两列数据框 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和项目ID之间的制表符距离。这不像表那样对齐。我更多的是Java和Python的背景知识,R中用于一般用途的底层编写命令是什么 有点凌乱,但这会起作用,将输

我有两列数据框

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和项目ID之间的制表符距离。这不像表那样对齐。我更多的是Java和Python的背景知识,R中用于一般用途的底层编写命令是什么

有点凌乱,但这会起作用,将输出写入
output.txt

d <- read.table(text='userID itemID
1       101
1       103
1       107
2       102
2       103
3       104', header=T)

cat(sapply(split(d, d$userID), function(x) 
  paste(x$userID[1], paste(x$itemID, collapse=' , '), sep='\t')), 
  sep='\n', file='output.txt')

d有点凌乱,但这会起作用,将输出写入
output.txt

d <- read.table(text='userID itemID
1       101
1       103
1       107
2       102
2       103
3       104', header=T)

cat(sapply(split(d, d$userID), function(x) 
  paste(x$userID[1], paste(x$itemID, collapse=' , '), sep='\t')), 
  sep='\n', file='output.txt')

d有点凌乱,但这会起作用,将输出写入
output.txt

d <- read.table(text='userID itemID
1       101
1       103
1       107
2       102
2       103
3       104', header=T)

cat(sapply(split(d, d$userID), function(x) 
  paste(x$userID[1], paste(x$itemID, collapse=' , '), sep='\t')), 
  sep='\n', file='output.txt')

d有点凌乱,但这会起作用,将输出写入
output.txt

d <- read.table(text='userID itemID
1       101
1       103
1       107
2       102
2       103
3       104', header=T)

cat(sapply(split(d, d$userID), function(x) 
  paste(x$userID[1], paste(x$itemID, collapse=' , '), sep='\t')), 
  sep='\n', file='output.txt')

d您可以为此使用dplyr包

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)
库(dplyr)

df.summary您可以为此使用dplyr包

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)
库(dplyr)

df.summary您可以为此使用dplyr包

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)
库(dplyr)

df.summary您可以为此使用dplyr包

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)
库(dplyr)

df.summary下面是另一个使用聚合的基本解决方案:

> write.table(aggregate(d$itemID, list(d$userID), paste, collapse=' , '),
              file='result.txt', sep='\t', col.names=FALSE, row.names=FALSE, quote=FALSE)
1       101 , 103 , 107
2       102 , 103
3       104

下面是另一个使用聚合的基本解决方案:

> write.table(aggregate(d$itemID, list(d$userID), paste, collapse=' , '),
              file='result.txt', sep='\t', col.names=FALSE, row.names=FALSE, quote=FALSE)
1       101 , 103 , 107
2       102 , 103
3       104

下面是另一个使用聚合的基本解决方案:

> write.table(aggregate(d$itemID, list(d$userID), paste, collapse=' , '),
              file='result.txt', sep='\t', col.names=FALSE, row.names=FALSE, quote=FALSE)
1       101 , 103 , 107
2       102 , 103
3       104

下面是另一个使用聚合的基本解决方案:

> write.table(aggregate(d$itemID, list(d$userID), paste, collapse=' , '),
              file='result.txt', sep='\t', col.names=FALSE, row.names=FALSE, quote=FALSE)
1       101 , 103 , 107
2       102 , 103
3       104