df2 <- structure(list(location = c("Dayton", "Toledo"), total_voters = c(236L,
332L), candidate_1 = c(49L, 61L), candidate_2 = c(33L, 78L),
candidate_3 = c(19L, 71L), candidate_5 = c(42L, 52L)), row.names = c(NA,
-2L), class = "data.frame")
我有来自SQL查询的数据,其形状如下:
+----------+--------------+-------------+-------------+-------------+-------------+-------------+
| location | total_voters | candidate_1 | candidate_2 | candidate_3 | candidate_4 | candidate_5 |
+----------+--------------+-------------+-------------+-------------+-------------+-------------+
| Dayton | 236 | 49 | 33 | 19 | 93 | 42 |
| Toledo | 332 | 61 | 78 | 71 | 70 | 52 |
+----------+--------------+-------------+-------------+-------------+-------------+-------------+
数字代表每个候选人的票数。那我想做什么?我想使用R(我想通过dplyr或tidyr)来旋转数据,使其看起来像这样:
+-------------+-------+----------+--------------+
| candidate | votes | location | total_voters |
+-------------+-------+----------+--------------+
| candidate_1 | 49 | Dayton | 236 |
| candidate_2 | 33 | Dayton | 236 |
| candidate_3 | 19 | Dayton | 236 |
| candidate_4 | 93 | Dayton | 236 |
| candidate_5 | 42 | Dayton | 236 |
| candidate_1 | 61 | Toledo | 332 |
| candidate_2 | 78 | Toledo | 332 |
| candidate_3 | 71 | Toledo | 332 |
| candidate_4 | 70 | Toledo | 332 |
| candidate_5 | 52 | Toledo | 332 |
+-------------+-------+----------+--------------+
在R中完成此操作的最有效方法是什么?
答案 0 :(得分:2)
这是pivot_longer
library(dplyr)
library(tidyr)
df1 %>%
pivot_longer(cols = everything(), names_to = 'candidate', values_to = 'votes')
# A tibble: 5 x 2
# candidate votes
# <chr> <dbl>
#1 candidate_1 49
#2 candidate_2 33
#3 candidate_3 19
#4 candidate_4 93
#5 candidate_5 42
使用更新的数据,
df2 %>%
pivot_longer(cols = -c(location, total_voters),
names_to = 'candidate', values_to = 'votes')
# A tibble: 8 x 4
# location total_voters candidate votes
# <chr> <int> <chr> <int>
#1 Dayton 236 candidate_1 49
#2 Dayton 236 candidate_2 33
#3 Dayton 236 candidate_3 19
#4 Dayton 236 candidate_5 42
#5 Toledo 332 candidate_1 61
#6 Toledo 332 candidate_2 78
#7 Toledo 332 candidate_3 71
#8 Toledo 332 candidate_5 52
或者在base R
中,可以使用stack
stack(df1)[2:1]
或通过转换为table
as.data.frame.table(as.matrix(df1))[,-1]
或按照@markus的建议
reshape2::melt(df1)
df1 <- data.frame(candidate_1 = 49, candidate_2 = 33,
candidate_3 = 19, candidate_4 = 93, candidate_5 = 42)
df2 <- structure(list(location = c("Dayton", "Toledo"), total_voters = c(236L,
332L), candidate_1 = c(49L, 61L), candidate_2 = c(33L, 78L),
candidate_3 = c(19L, 71L), candidate_5 = c(42L, 52L)), row.names = c(NA,
-2L), class = "data.frame")
答案 1 :(得分:0)
实际上,您可以使用data.frame
+ t
进行修改,即
dflong <- data.frame(t(dfwide))
答案 2 :(得分:0)
如果候选人_1候选人_2等是列名,则可以使用reshape2软件包中的melt函数。
a=data.frame(candidate_1=49,
candidate_2=33,
candidate_3=19,
candidate_4=93,
candidate_5=42)
b=reshape2::melt(a)