所以我的数据显示两个具有相同数据的变量(X和PassengerId)
> str(myData)
'data.frame': 500 obs. of 13 variables:
$ X : int 1 2 5 6 7 8 9 10 11 12 ...
$ PassengerId: int 1 2 5 6 7 8 9 10 11 12 ...
$ Survived : int 0 1 0 0 0 0 1 1 1 1 ...
$ Pclass : int 3 1 3 3 1 3 3 2 3 1 ...
$ Name : Factor w/ 891 levels "Abbing, Mr. Anthony",..: 109 191 16 559 520 629 417 581 732 96 ...
$ Sex : Factor w/ 2 levels "female","male": 2 1 2 2 2 2 1 1 1 1 ...
$ Age : num 22 38 35 NA 54 2 27 14 4 58 ...
$ SibSp : int 1 1 0 0 0 3 0 1 1 0 ...
$ Parch : int 0 0 0 0 0 1 2 0 1 0 ...
$ Ticket : Factor w/ 681 levels "110152","110413",..: 524 597 473 276 86 396 345 133 617 39 ...
$ Fare : num 7.25 71.28 8.05 8.46 51.86 ...
$ Cabin : Factor w/ 147 levels "A10","A14","A16",..: NA 82 NA NA 130 NA NA NA 146 50 ...
$ Embarked : Factor w/ 3 levels "C","Q","S": 3 1 3 2 3 3 3 1 3 3 ...
如何从数据中删除变量X?
答案 0 :(得分:0)
dplyr::select(mydata, -X)
应该做到这一点。 documentation有很好的示例说明如何使用select
删除变量。
答案 1 :(得分:0)
除此之外,这样的东西也有效。
myData[,-1] # in base R
您也可以将数据框转换为如下表格。
DT <- copy(myData)
DT <- setDT(DT,keep.rownames=T)
DT[,-1] # this will remove the unnecessary column you need