[英]Extracting a specific type columns and specific named columns from a data frame-R
讓我有一個數據框,其中有一些列系數因子類型,並且有一個名為“索引”的列,它不是列。 我想提取列
例如讓
df<-data.frame(a=runif(10),b=as.factor(sample(10)),index=as.numeri(1:10))
所以df是:
a b index
0.16187501 5 1
0.75214741 8 2
0.08741729 3 3
0.58871514 2 4
0.18464752 9 5
0.98392420 1 6
0.73771960 10 7
0.97141474 6 8
0.15768011 7 9
0.10171931 4 10
所需的輸出是(讓它是一個稱為df1的數據幀)
DF1:
b index
5 1
8 2
3 3
2 4
9 5
1 6
10 7
6 8
7 9
4 10
其中包括因子列和名為“索引”的列。
我用這樣的代碼
vars<-apply(df,2,function(x) {(is.factor(x)) || (names(x)=="index")})
df1<-df[,vars]
但是,此代碼不起作用。 如何在R中使用Apply類型函數返回df1? 我會很高興為您提供任何幫助。 非常感謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.