简体   繁体   中英

Select column numbers only from table in R

I am new using R, and I have a problem when reading data. I am reading a .csv file:

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

2 rich bright 4 AA Bb 48.5 rough 10.43

3 rich bright 2 Aa Bb 47 smooth 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. How can I select just the column numeric values?

Use header=TRUE in your read.table call. Or just use read.csv as it defaults to 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. 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]) . Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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