简体   繁体   English

跳过R read.table()中的列

[英]Skipping columns in R read.table()

I wanna skip the first three columns. 我想跳过前三列。 Couldn't quite understand the posts about colClasses because I'm new to R. 不太了解有关colClasses的帖子,因为我是R的新手。

YDL025C YDL025C 1   -0.1725 -0.5375 -0.4970 -0.3818 -0.5270 -0.4260 -0.6929 -0.4020 -0.3263 -0.3373 -0.3532 -0.2771 -0.2732 -0.3307 -0.4660 -0.4314 -0.3135
YKL032C YKL032C 1   -0.2364 0.0794  0.1678  0.2389  0.3847  0.2625  0.1889  0.2681  0.0363  -0.1992 -0.0521 -0.0307 0.0584  0.2817  0.2239  -0.0253 0.0751

If you have to use read.table and you want to filter on the way in, you can use col.classes as follows. 如果必须使用read.table并希望进行过滤,则可以按如下方式使用col.classes。 You have 20 columns. 您有20列。 Say the first 2 are character, rest are numeric and you want to drop 4,5,6. 假设前2个是字符,其余是数字,并且您想删除4,5,6。 You construct a vector of length 20 detailing that information. 您构造一个长度为20的向量,详细说明该信息。 The NULL will not pull in those columns. NULL不会拉入那些列。

x<- read.table(file="datat.txt", 
               colClasses = c(rep("character", 2),
                              rep("numeric", 1),
                              rep("NULL", 3),
                              rep("numeric", 14)),
               header = FALSE)
x

       V1      V2 V3      V7      V8      V9     V10     V11     V12     V13     V14     V15     V16     V17     V18     V19     V20
1 YDL025C YDL025C  1 -0.3818 -0.5270 -0.4260 -0.6929 -0.4020 -0.3263 -0.3373 -0.3532 -0.2771 -0.2732 -0.3307 -0.4660 -0.4314 -0.3135
2 YKL032C YKL032C  1  0.2389  0.3847  0.2625  0.1889  0.2681  0.0363 -0.1992 -0.0521 -0.0307  0.0584  0.2817  0.2239 -0.0253  0.0751

As commented above, easier to remove the columns after reading in. For example: 如上所述,读入后更容易除去列。例如:

mydf <- read.table("mydf.txt")

Then, 然后,

mydf[, 4:ncol(mydf)]

will remove the first 3 columns. 将删除前3列。

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

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