简体   繁体   English

使用write.table获取数据帧,但不包含R中每一行的起始部分

[英]Obtain a data frame using write.table without initial part of every row in R

I have a data frame in which rows are like this: xyz-1; 我有一个数据框,其中的行是这样的:xyz-1; xyz-2; XYZ-2; and so on 等等

data frame x: 数据框x:

      1 2 3
xyz-1 1 4 e
xyz-2 2 5 a
xyz-3 3 6 c

I tried write.table(x,file="out") ; 我试过了write.table(x,file="out") ; x is my data frame. x是我的数据框。 When i use write.table , i want the text file (output) whose rows are in the form: 1;2;...; 当我使用write.table ,我想要文本文件(输出),其行格式为:1; 2; ...;

How can i do this? 我怎样才能做到这一点? How can i remove the initial part of every row (xyz)? 我如何删除每一行的起始部分(xyz)?

You can remove the "xyz - " part with: rownames(x) <- NULL . 您可以使用以下方法删除“ xyz-”部分: rownames(x) <- NULL

Alternative: rownames(x) <- gsub("xyz-", "", rownames(x)) This replaces "xyz-" with "" (nothing) so it removes it. 备选: rownames(x) <- gsub("xyz-", "", rownames(x))这将“ xyz-”替换为“”(什么都没有),因此将其删除。

Make sure the pattern is "xyz-" exactly, or if you need a space somewhere (like "xyz -"). 确保模式完全是“ xyz-”,或者如果您需要在某处留空格(例如“ xyz-”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM