我所有的专栏都是行格式。我如何更改数据框以根据每个团队 ID 将每一行制作成一列
All my columns are in row format. How can i change the dataframe to have each row made into a column, based on each Team ID
我正在尝试为我想做的一些足球分析清理一些数据。我的问题是我不知道如何正确更改我的数据框。没有图,实在是没法好好解释了。
这是我的数据的屏幕截图。
我希望数据看起来像这样
列:团队 XG 进攻 XG 防守
A 行:A 队关联 Measure.Value 关联 Measure.Value
B 组:…………
为演示目的创建数据框
df <- structure(
list(
Measure.Names = structure(
c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L),
.Label = c("Over Perform XG - Defense", "Over Perform XG - Offense"),
class = "factor"
),
Team = structure(
c(4L, 3L, 2L, 1L, 4L,
3L, 2L, 1L),
.Label = c(
"Bournemouth",
"Brighton and Hove Albion",
"Burnley",
"Cardiff City"
),
class = "factor"
),
Measure.Values = structure(
1:8,
.Label = c("1",
"2", "3", "4", "5", "6", "7", "8"),
class = "factor"
)
),
class = "data.frame",
row.names = c(NA,-8L)
)
df
#> Measure.Names Team Measure.Values
#> 1 Over Perform XG - Defense Cardiff City 1
#> 2 Over Perform XG - Defense Burnley 2
#> 3 Over Perform XG - Defense Brighton and Hove Albion 3
#> 4 Over Perform XG - Defense Bournemouth 4
#> 5 Over Perform XG - Offense Cardiff City 5
#> 6 Over Perform XG - Offense Burnley 6
#> 7 Over Perform XG - Offense Brighton and Hove Albion 7
#> 8 Over Perform XG - Offense Bournemouth 8
用户tidyr::spread转置
tidyr::spread(df, Measure.Names, Measure.Values)
#> Team Over Perform XG - Defense
#> 1 Bournemouth 4
#> 2 Brighton and Hove Albion 3
#> 3 Burnley 2
#> 4 Cardiff City 1
#> Over Perform XG - Offense
#> 1 8
#> 2 7
#> 3 6
#> 4 5
由 reprex package (v0.2.0) 创建于 2019-02-06。
我正在尝试为我想做的一些足球分析清理一些数据。我的问题是我不知道如何正确更改我的数据框。没有图,实在是没法好好解释了。
这是我的数据的屏幕截图。
我希望数据看起来像这样
列:团队 XG 进攻 XG 防守
A 行:A 队关联 Measure.Value 关联 Measure.Value
B 组:…………
为演示目的创建数据框
df <- structure(
list(
Measure.Names = structure(
c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L),
.Label = c("Over Perform XG - Defense", "Over Perform XG - Offense"),
class = "factor"
),
Team = structure(
c(4L, 3L, 2L, 1L, 4L,
3L, 2L, 1L),
.Label = c(
"Bournemouth",
"Brighton and Hove Albion",
"Burnley",
"Cardiff City"
),
class = "factor"
),
Measure.Values = structure(
1:8,
.Label = c("1",
"2", "3", "4", "5", "6", "7", "8"),
class = "factor"
)
),
class = "data.frame",
row.names = c(NA,-8L)
)
df
#> Measure.Names Team Measure.Values
#> 1 Over Perform XG - Defense Cardiff City 1
#> 2 Over Perform XG - Defense Burnley 2
#> 3 Over Perform XG - Defense Brighton and Hove Albion 3
#> 4 Over Perform XG - Defense Bournemouth 4
#> 5 Over Perform XG - Offense Cardiff City 5
#> 6 Over Perform XG - Offense Burnley 6
#> 7 Over Perform XG - Offense Brighton and Hove Albion 7
#> 8 Over Perform XG - Offense Bournemouth 8
用户tidyr::spread转置
tidyr::spread(df, Measure.Names, Measure.Values)
#> Team Over Perform XG - Defense
#> 1 Bournemouth 4
#> 2 Brighton and Hove Albion 3
#> 3 Burnley 2
#> 4 Cardiff City 1
#> Over Perform XG - Offense
#> 1 8
#> 2 7
#> 3 6
#> 4 5
由 reprex package (v0.2.0) 创建于 2019-02-06。