R中有沒有辦法選擇許多非連續的,即奇數或偶數的行/列?我正在為主成分分析繪制載荷。我有84行數據按如下順序排序:x_1 y_1 x_2.....x_42 y_42現在,我正在為x和y加載圖創建數據框,如下所示:data.pc = princomp(as.matrix(data))x.loadings <- data.frame(x=data.pc$loadings[c(1, 3, 5, 7, 9, 11, 13 ,15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41), 1])yloadings <- data.frame(y=data.pc$loadings[c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42), 1])當然有更簡單的方法可以做到這一點?
3 回答

子衿沉夜
TA貢獻1828條經驗 獲得超3個贊
您始終可以使用seq生成序列:
even_indexes<-seq(2,42,2)
odd_indexes<-seq(1,41,2)
然后,
x.loadings <- data.frame(x=data.pc$loadings[odd_indexes,1])

斯蒂芬大帝
TA貢獻1827條經驗 獲得超8個贊
當邏輯向量用于索引編制時,它們將被回收,以便獲得奇數列或奇數行
calld[ c(TRUE,FALSE), ] # rows
calld[ , c(TRUE,FALSE) ] #columns
偶數行或列:
calld[ !c(TRUE,FALSE), ] # rows
calld[ , !c(TRUE,FALSE) ] #columns
每三列:
calld[ , c(TRUE,FALSE, FALSE) ] #columns 1,4,7 , ....
- 3 回答
- 0 關注
- 664 瀏覽
添加回答
舉報
0/150
提交
取消