假设我有一个10 * 10矩阵--- A,其中列的名称和顺序与行(a,b,c,..,j)相同。由于计算矩阵的特征,我必须按行属性重新排序行和列,比如“attr”。假设a,c,e的attr等于1,attr为b,f,i,j等于2,attr为d,g,h等于3.我想出一种方法将行重新排序为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.sms_sending">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.SmsActivity"
android:label="@string/title_activity_sms"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
/>
<!-- SMS Receiver -->
<receiver android:name=".receiver.SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- Intent service -->
<service
android:name=".services.HttpService"
android:exported="false" />
</application>
现在我有一个新的矩阵Anew <- rbind(subset(A, attr==1), subset(A, attr==2), subset(A, attr==3))
,其中行的顺序是a,c,e,b,f,i,j,d,g,h,列的顺序保持不变作为a,...,j。我的问题是,如何按Anew
行的顺序重新排序Anew
中的列?我知道我可以使用
Anew
但有更有效的方法吗?如果矩阵是100 * 100,则上面的代码非常不足。谢谢。
答案 0 :(得分:3)
我们可以order
attr
并将其用作行索引
Anew <- A[order(as.numeric(attr(A, 'row.names'))),]
Anew
# a b c d e f g h i j
#a -0.545880758 -1.3169081 -0.07430856 -0.03373792 0.06735770 0.5266526 1.4520752 0.14379993 -0.571243248 -0.2327672
#c 0.419623149 -0.7622144 -1.70964518 0.61285136 -0.34365937 0.7696819 -0.4403340 -0.02557419 -1.673385810 -0.8133227
#e 0.847460017 0.3322444 -0.64859151 0.65738044 -0.25574457 -0.1961822 0.5713866 -0.07596102 0.361212489 -0.2148936
#b 0.536585304 0.5982691 -0.60515695 -0.58542756 0.01710596 -1.0736261 0.4082015 -0.88610999 0.422621775 -1.4203631
#f 0.266021979 -0.4690607 -0.09411013 -1.07418134 -0.46120796 0.2047497 -1.2799872 -1.35466363 1.056864213 0.1389452
#i -0.848370044 0.6099945 -0.11629639 0.16922669 0.33519430 1.0494212 1.3496121 -0.12316046 0.007165349 1.0451687
#j 0.002311942 0.5163357 -0.94382724 -1.82219032 -0.23186459 0.5609812 -1.5676166 0.00104102 0.101974336 1.2101739
#d -0.583627199 -1.4290903 -0.26869311 1.51712249 -0.66789220 1.7709054 1.3185662 -0.32773539 -1.136025931 0.4610693
#g 0.444585270 -0.3349868 -0.08554095 -4.46956441 1.47164158 -0.5965981 -1.2388796 -0.96080882 -1.992920453 0.8442304
#h -0.466495124 1.5362522 0.11953107 0.36904502 -0.09196032 1.1782477 -0.9225911 0.22495434 0.764385054 -1.3084503
如果我们想为行和列执行此操作
Anew1 <- A[order(as.numeric(attr(A, 'row.names'))), order(as.numeric(attr(A, 'col.names')))]
set.seed(24)
A <- matrix(rnorm(10*10), ncol=10, dimnames=list(letters[1:10], letters[1:10]))
attr(A, 'row.names') <- as.character(c(1, 2, 1,3, 1, 2, 3, 3, 2, 2))
attr(A, 'col.names') <- as.character(c(1, 2, 1,3, 1, 2, 3, 3, 2, 2))