简体   繁体   English

仅从R中的表中选择列号

[英]Select column numbers only from table in R

I am new using R, and I have a problem when reading data. 我是R的新手,读取数据时遇到问题。 I am reading a .csv file: 我正在读取.csv文件:

table<- read.table("/Users/.../data.plants.csv", header=FALSE, sep=";")

The table has the format: 该表的格式为:

      V1              V2                    V3         V4         V5     V6        V7        V8

1 nutrient light microsatellite_length genotype_A genotype_B height leaf_type leaf_size 1个营养光微卫星_长度基因型_A基因型_B高度叶_叶型_大小

2 rich bright 4 AA Bb 48.5 rough 10.43 2富亮4 AA Bb 48.5粗糙10.43

3 rich bright 2 Aa Bb 47 smooth 6.54 3富亮2 Aa Bb 47光滑6.54

....(continues) ....(续)

I want to just select one column, the column that has leaf size. 我只想选择一列,即具有叶子大小的列。 I am doing it like this: 我这样做是这样的:

x<-subset(table,select=c(V8)) 

It has a problem, it also selects the header ("leaf_size") and I want just the numeric values. 它有问题,它还会选择标题(“ leaf_size”),而我只需要数字值。 How can I select just the column numeric values? 如何仅选择列数值?

Use header=TRUE in your read.table call. read.table调用中使用header=TRUE Or just use read.csv as it defaults to this. 或仅使用read.csv因为它默认为this。

plants <- read.csv("/Users/.../data.plants.csv")

To add to the first answer, by default, read.csv results in strings being classified as factors. 要添加到第一个答案,默认情况下,read.csv会将字符串分类为因素。 If you do not want this (and generally, you don't), one uses: 如果您不希望这样做(通常不希望这样做),请使用:

read.table('plants.csv', stringsAsFactors=FALSE, sep=';') -> plants
plants[,6]

will then give you the sixth column as strings. 然后将给您第六列作为字符串。 I'll assume you want it as numbers, given by as.numeric(plants[,6]) . 我假设您希望将其作为数字,由as.numeric(plants[,6]) Hope that helps! 希望有帮助!

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

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