简体   繁体   中英

R: “Error in `[.data.frame ” and “unused arguments”

I have a vector of 20 elements and i want that only 15 elements are assigned to a new vector. When I do that, R poped me up an error, and I tried different combinations. I want to assign elements from 6 to 19.

> carbo<-read.csv(file="6cwga", header=TRUE, sep=",")
> carbo2 <- carbo[-1,-2,-3,-4,-5,,-20]
Error in `[.data.frame`(carbo, -1, -2, -3, -4, -5, , -20) : 
  unused arguments (-4, -5, , -20)
> carbo2 <- carbo[-1,-2,-3,-4,-5,-20]
Error in `[.data.frame`(carbo, -1, -2, -3, -4, -5, -20) : 
  unused arguments (-4, -5, -20)
> carbo2 <- carbo[-1,-2,-3,-4,-5,-20,]
Error in `[.data.frame`(carbo, -1, -2, -3, -4, -5, -20, ) : 
  unused arguments (-4, -5, -20, )

You didn't subset the data frame properly. The general form to subset a data frame is <dataframe_name>(row,column) . To subset using multiple rows or columns you have to combine them using c() .

See below for corrected code.

carbo<-read.csv(file="6cwga", header=TRUE, sep=",")
## to select the columns -1, -2, -3, -4, -5, -20
carboselectcol <- carbo[,c(-1,-2,-3,-4,-5,-20)]

## to select the numbers as rows.
carboselectrow<- carbo[c(-1,-2,-3,-4,-5,-20),]

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