Search code examples
rvectorassign

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, )

Solution

  • 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),]