如何将数据框的一列拆分为 R 中的新列?
How to split up a column of a dataframe into new columns in R?
我有一个包含一列和 n 行的数据框,如下所示:
data.frame(rep(x=c("c","a","c","b","c","d"),times=c(1,4,1,4,1,4)))
现在,我想拆分数据框的这一列,每个 c
创建一个新列。目的是将只有一列的数据框转换成这种形式:
c
c
c
a
b
d
a
b
d
a
b
d
a
b
d
使用 tidyverse
,我们可以在每次 c
出现在 x
列时创建一个新组,然后我们可以将数据旋转到宽范围。通常,不鼓励重复名称,因此我创建了一个顺序 c
列名。
library(tidyverse)
results <- df %>%
group_by(idx = cumsum(x == "c")) %>%
filter(x != "c") %>%
mutate(rn = row_number()) %>%
pivot_wider(names_from = idx, values_from = x, names_prefix = "c_") %>%
select(-rn)
输出
c_1 c_2 c_3
<chr> <chr> <chr>
1 a b d
2 a b d
3 a b d
4 a b d
但是,如果您真的想要重复的名称,那么我们可以添加 set_names
:
purrr::set_names(results, "c")
c c c
<chr> <chr> <chr>
1 a b d
2 a b d
3 a b d
4 a b d
或者在 base R 中,我们可以用 cumsum
创建分组,然后拆分这些组,然后用 cbind
绑定回去。然后,我们删除包含 c
个字符的第一行。
names(df) <- "c"
do.call(cbind, split(df, cumsum(df$c == "c")))[-1,]
# c c c
#2 a b d
#3 a b d
#4 a b d
#5 a b d
您的列具有相同数量的值,如给出的示例所示:
unstack(df, x ~ cumsum(x=="c"))
X1 X2 X3
1 c c c
2 a b d
3 a b d
4 a b d
5 a b d
然后您可以删除第一行
我有一个包含一列和 n 行的数据框,如下所示:
data.frame(rep(x=c("c","a","c","b","c","d"),times=c(1,4,1,4,1,4)))
现在,我想拆分数据框的这一列,每个 c
创建一个新列。目的是将只有一列的数据框转换成这种形式:
c | c | c |
---|---|---|
a | b | d |
a | b | d |
a | b | d |
a | b | d |
使用 tidyverse
,我们可以在每次 c
出现在 x
列时创建一个新组,然后我们可以将数据旋转到宽范围。通常,不鼓励重复名称,因此我创建了一个顺序 c
列名。
library(tidyverse)
results <- df %>%
group_by(idx = cumsum(x == "c")) %>%
filter(x != "c") %>%
mutate(rn = row_number()) %>%
pivot_wider(names_from = idx, values_from = x, names_prefix = "c_") %>%
select(-rn)
输出
c_1 c_2 c_3
<chr> <chr> <chr>
1 a b d
2 a b d
3 a b d
4 a b d
但是,如果您真的想要重复的名称,那么我们可以添加 set_names
:
purrr::set_names(results, "c")
c c c
<chr> <chr> <chr>
1 a b d
2 a b d
3 a b d
4 a b d
或者在 base R 中,我们可以用 cumsum
创建分组,然后拆分这些组,然后用 cbind
绑定回去。然后,我们删除包含 c
个字符的第一行。
names(df) <- "c"
do.call(cbind, split(df, cumsum(df$c == "c")))[-1,]
# c c c
#2 a b d
#3 a b d
#4 a b d
#5 a b d
您的列具有相同数量的值,如给出的示例所示:
unstack(df, x ~ cumsum(x=="c"))
X1 X2 X3
1 c c c
2 a b d
3 a b d
4 a b d
5 a b d
然后您可以删除第一行