你如何 pivot_wider where values are characters?
How do you pivot_wider where values are characters?
我想使用 pivot_wider 或在值是字符串的地方展开。
这是一个例子:
a <- c("a", "a", "b", "c", "c", "c")
x <- c("x", "y", "y", "x", "y", "z")
m <- cbind(a,x)
> m
a x
[1,] "a" "x"
[2,] "a" "y"
[3,] "b" "y"
[4,] "c" "x"
[5,] "c" "y"
[6,] "c" "z"
我想要的输出是:
V1 V2 V3 V4
one a x y <NA>
two b y <NA> <NA>
three c x y z
然而,我的老去,传播,不起作用:
> as.data.frame(m) %>% spread(key = a, value = x)
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 5 rows:
* 1, 2
* 4, 5, 6
也不 pivot_wider:
as.data.frame(m) %>%
group_by(a) %>%
mutate(namer = n()) %>%
ungroup() %>%
pivot_wider(#id_cols = a,
names_from = namer,
values_from = x,
values_fill = list(namer = "none"),
names_prefix = "City")
有什么建议吗?
@akrun 感谢您的回答,那里发生了很多事情,这是我将其缩小到的范围:
m <- data.frame(a, x)
m %>% group_by(a) %>%
mutate(rn = str_c("V", row_number()+1)) %>%
ungroup %>%
pivot_wider(names_from = rn, values_from = x)
有效。但是,
m %>%
group_by(a) %>%
mutate(rn = n()) %>%
ungroup %>%
pivot_wider(names_from = rn, values_from = x, names_prefix = "V")
没有。所以问题似乎是我使用数字作为名称,并使用 names_prefix 将它们转换为字符。你也是这样理解的吗?
我们可以创建 data.frame
而不是 matrix
(cbind
创建 matrix
)
library(dplyr)
library(tidyr)
library(stringr)
library(english)
m %>%
group_by(a) %>%
mutate(rn = str_c("V", row_number()+1)) %>%
ungroup %>%
rename(V1 = a) %>%
pivot_wider(names_from = rn, values_from = x) %>%
mutate(rn = as.character(english(row_number()))) %>%
column_to_rownames('rn')
# V1 V2 V3 V4
#one a x y <NA>
#two b y <NA> <NA>
#three c x y z
数据
m <- data.frame(a, x)
我想使用 pivot_wider 或在值是字符串的地方展开。
这是一个例子:
a <- c("a", "a", "b", "c", "c", "c")
x <- c("x", "y", "y", "x", "y", "z")
m <- cbind(a,x)
> m
a x
[1,] "a" "x"
[2,] "a" "y"
[3,] "b" "y"
[4,] "c" "x"
[5,] "c" "y"
[6,] "c" "z"
我想要的输出是:
V1 V2 V3 V4
one a x y <NA>
two b y <NA> <NA>
three c x y z
然而,我的老去,传播,不起作用:
> as.data.frame(m) %>% spread(key = a, value = x)
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 5 rows:
* 1, 2
* 4, 5, 6
也不 pivot_wider:
as.data.frame(m) %>%
group_by(a) %>%
mutate(namer = n()) %>%
ungroup() %>%
pivot_wider(#id_cols = a,
names_from = namer,
values_from = x,
values_fill = list(namer = "none"),
names_prefix = "City")
有什么建议吗?
@akrun 感谢您的回答,那里发生了很多事情,这是我将其缩小到的范围:
m <- data.frame(a, x)
m %>% group_by(a) %>%
mutate(rn = str_c("V", row_number()+1)) %>%
ungroup %>%
pivot_wider(names_from = rn, values_from = x)
有效。但是,
m %>%
group_by(a) %>%
mutate(rn = n()) %>%
ungroup %>%
pivot_wider(names_from = rn, values_from = x, names_prefix = "V")
没有。所以问题似乎是我使用数字作为名称,并使用 names_prefix 将它们转换为字符。你也是这样理解的吗?
我们可以创建 data.frame
而不是 matrix
(cbind
创建 matrix
)
library(dplyr)
library(tidyr)
library(stringr)
library(english)
m %>%
group_by(a) %>%
mutate(rn = str_c("V", row_number()+1)) %>%
ungroup %>%
rename(V1 = a) %>%
pivot_wider(names_from = rn, values_from = x) %>%
mutate(rn = as.character(english(row_number()))) %>%
column_to_rownames('rn')
# V1 V2 V3 V4
#one a x y <NA>
#two b y <NA> <NA>
#three c x y z
数据
m <- data.frame(a, x)