如何将CSV文件转换为R中的.txt文件(How to convert CSV file to .txt file in R)

编程入门 行业动态 更新时间:2024-10-24 20:19:49
如何将CSV文件转换为R中的.txt文件(How to convert CSV file to .txt file in R)

给定一个巨大的CSV文件,我想只读取它的前2000行,并将其保存为.txt文件。

data<-read.csv("myfile", header=T, sep=",", nrow=2000)

Given a huge CSV file, I want to read just the first 2000 lines of it and save it as a .txt file.

data<-read.csv("myfile", header=T, sep=",", nrow=2000)

最满意答案

在Linux / MacOS下,您可以使用head来执行此操作而无需读取文件:

head -n 2001 source_file > target_file

这假设有一个头,因此文件中的前2001行放在target_file 。 在Windows下,您可以通过安装Cygwin或MinGW 。

为了解决你在R中的问题,你已经在R中有了内容。只需使用write.csv将数据写入文件即可。

Under Linux/MacOS you can use head to do this without having to read the file:

head -n 2001 source_file > target_file

This assumes that there is a header, therefore the first 2001 rows in the file are put in target_file. Under Windows you can get this working by installing Cygwin or MinGW.

For solving your problem in R, you already have the content in R. Just use write.csv to write the data to a file.

更多推荐

本文发布于:2023-07-24 01:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   转换为   如何将   CSV   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!