我有一个实时捕获的数据框。有两个系统,每个系统具有不同的状态。每个星期和每个州的人数都给出了。
df <- data.frame (sys = rep(c("a","b"), each=3),
sta = rep(c("L","T","P"), times=2),
w01 = c("0","2","5","2","2","5"),
w02 = c("3","2","2","4","6","1"),
w03 = c("4","1","0","5","3","0"))
df
sys sta w01 w02 w03
a L 0 3 4
a T 2 2 1
a P 5 2 0
b L 2 4 5
b T 2 6 3
b P 5 1 0
我想要一个数据框显示每个数字的系统和状态。结果如下所示:
sys sta Num
a L 0
a L 3
a L 4
a T 2
a T 2
a T 1
a P 5
a P 2
a P 0
b L 2
b L 4
b L 5
b T 2
b T 6
b T 3
b P 5
b P 1
b P 0
我完全不知道如何做到这一点。我发现了这两个问题/答案
Reorganise 2x36 dataframe to a 6x6 dataframe. Dice throw visualisation
Reshaping data.frame from wide to long format
但无法找到适合我的问题的方法。
我也尝试了reshape
,但direction = "wide"
没有提供我需要的输出,而且我被direction = "long"
的不同部分所吸引。这是我试过的,但没有用......
a <- as.vector(colnames(df[-c(1,2)]))
df2 <- reshape(df, idvar = "sys", timevar = "sta", varying = a , direction = "long")
有什么建议我可以解决这个问题吗? 谢谢大家和亲切的问候!
P.S。:我需要&#34;新格式&#34;我的数据集的弗里德曼测试(friedman.test
)。它会以这种方式运作吗?
答案 0 :(得分:0)
tidyr
和dplyr
:
library(tidyr)
library(dplyr)
df %>%
gather(key, value, -sys, -sta) %>%
select(-key) %>%
arrange(sys, sta)
#> sys sta value
#> 1 a L 0
#> 2 a L 3
#> 3 a L 4
#> 4 a P 5
#> 5 a P 2
#> 6 a P 0
#> 7 a T 2
#> 8 a T 2
#> 9 a T 1
#> 10 b L 2
#> 11 b L 4
#> 12 b L 5
#> 13 b P 5
#> 14 b P 1
#> 15 b P 0
#> 16 b T 2
#> 17 b T 6
#> 18 b T 3
答案 1 :(得分:0)
您可以使用melt
reshape2
melt(df, id.vars = c("sys","sta"), value.name = "num")
# sys sta variable num
# 1 a L w01 0
# 2 a T w01 2
# 3 a P w01 5
# 4 b L w01 2
# 5 b T w01 2
# 6 b P w01 5
# 7 a L w02 3
# 8 a T w02 2
# 9 a P w02 2
# 10 b L w02 4
# 11 b T w02 6
# 12 b P w02 1
# 13 a L w03 4
# 14 a T w03 1
# 15 a P w03 0
# 16 b L w03 5
# 17 b T w03 3
# 18 b P w03 0
如果您要删除variable
列,请使用
melt(df, id.vars = c("sys","sta"), value.name = "num")[,c(1,2,4)]
答案 2 :(得分:0)
以下是tidyverse
方法:
它是tidyr
tidyverse
包的一部分
library(tidyverse)
out <- gather(df, key = 'week', value = 'number', -sys, -sta)
输出:
sys sta week number
1 a L w01 0
2 a T w01 2
3 a P w01 5
4 b L w01 2
5 b T w01 2
6 b P w01 5
7 a L w02 3
8 a T w02 2
9 a P w02 2
10 b L w02 4
11 b T w02 6
12 b P w02 1
13 a L w03 4
14 a T w03 1
15 a P w03 0
16 b L w03 5
17 b T w03 3
18 b P w03 0
工作原理:
gather
获取未排除的所有列。通过将select
语句传递给...
参数来处理排除。在这种情况下,我将sys
和sta
排除在外。因此,我们gather
all_w
唯一的列是key
。value
收集一个名称,并为value
字段命名。键字段将包含输出的每个元素(行)的列名称。 week
将包含该列中行的内容。df %>% select(-week)
,以便稍后了解产生您的某个号码的曝光时间。如果没有,您可以使用spread(out, key = week, value = number)
删除它。df
。瞧,你的旧public GPSTracker(Activity activity) {
this.mActivity = activity;
getLocation();
}
public void getLocation() {
try {
locationManager = (LocationManager) mActivity
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled || !isNetworkEnabled) {
showSettingsAlert();
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to ic_check GPS/wifi enabled
*
* @return boolean
*/
public boolean canGetLocation() {
return this.canGetLocation;
}
!