X Income Limit Rating Cards Age Education Gender Student Married Ethnicity Balance
1 1 14.891 3606 283 2 34 11 Male No Yes Caucasian 333
2 2 106.025 6645 483 3 82 15 Female Yes Yes Asian 903
3 3 104.593 7075 514 4 71 11 Male No No Asian 580
4 4 148.924 9504 681 3 36 11 Female No No Asian 964
5 5 55.882 4897 357 2 68 16 Male No Yes Caucasian 331
6 6 80.180 8047 569 4 77 10 Male No No Caucasian 1151
以上是我数据框的主管。我有一个12列的df。这些cols的类型为"整数","数字"或"因素"。我希望创建一个子集数据框,其中只包含类型"整数"的cols。或"字符"。
我试过这段代码,但它没有用。我更喜欢使用数据框的答案,尽管我试图使用矩阵。
z <- matrix(data = NA, nrow = 400 , ncol = 8 )
for(i in 2: dim(credit.csv)[2]) {
if (class(credit.csv[2,i]) == "integer" | "numeric")
{
z[,i] <- credit.csv[,i]; i = i +1
else (skip)
}
}
> str(credit.csv)
'data.frame': 400 obs. of 12 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ Income : num 14.9 106 104.6 148.9 55.9 ...
$ Limit : int 3606 6645 7075 9504 4897 8047 3388 7114 3300 6819 ...
$ Rating : int 283 483 514 681 357 569 259 512 266 491 ...
$ Cards : int 2 3 4 3 2 4 2 2 5 3 ...
$ Age : int 34 82 71 36 68 77 37 87 66 41 ...
$ Education: int 11 15 11 11 16 10 12 9 13 19 ...
$ Gender : Factor w/ 2 levels " Male","Female": 1 2 1 2 1 1 2 1 2 2 ...
$ Student : Factor w/ 2 levels "No","Yes": 1 2 1 1 1 1 1 1 1 2 ...
$ Married : Factor w/ 2 levels "No","Yes": 2 2 1 1 2 1 1 1 1 2 ...
$ Ethnicity: Factor w/ 3 levels "African American",..: 3 2 2 2 3 3 1 2 3 1 ...
$ Balance : int 333 903 580 964 331 1151 203 872 279 1350 ...
答案 0 :(得分:0)
如果只想要整数/数字列,则排除字符列
credit.Int = data [,! %c中的姓名(数据)%(“性别”,“已婚”,“种族”)]
所以这是你的数字数据框
您可以对字符数据框执行相反的操作