将单行数据转换为包含它们组合数值的多行数据。

huangapple go评论78阅读模式
英文:

Pivoting single rows into rows with their combined values

问题

I can help you with the translation. Here's the translated text:

我想将列word转换为另一列,但原始行要以_分隔方式合并在一起。

原始内容:

> head(df, 10)
# A tibble: 10 x 1
   word    
   <chr>   
 1 Jason   
 2 Oscar   
 3 Maleeka 
 4 Janet   
 5 Gabriel 
 6 Raheema 
 7 Bryce   
 8 Nasreen 
 9 Hishaam 
10 Thadduse

期望的输出:

word 
Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
.
.
.

问题:

  1. 如何将word列旋转,以便每一行包含10个单词,以"_"分隔?(Tidyverse和stringr方法将不胜感激 - 谢谢!)

数据:

> dput(df)
structure(list(word = c("Jason", "Oscar", "Maleeka", "Janet", 
"Gabriel", "Raheema", "Bryce", "Nasreen", "Hishaam", "Thadduse", 
"Marcos", "Daijah", "Chassity", "Carlito", "Chidiebere", "Matthew", 
"Maureene", "Jillian", "Markus", "Aaron", "Ramziyya", "Marquez", 
"Kiera", "Farajallah", "Larisa", "Davier", "Shujaa", "Vincent", 
"Orlando", "Joseph", "Desean", "Chelsea", "Faadil", "Christopher", 
"Aarifa", "Joel", "Matthew", "Jacob", "Aeones", "Matthew", "Jacob", 
"Savannah", "Nadia", "Kaleem", "Tanner", "Sabeeha", "Caitlyn", 
"Taylor", "Sydney", "Devin")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -50L))
英文:

I would like to transform the column word into another column, but with the original rows combined together, separed by _.

  • Original:
> head(df, 10)
# A tibble: 10 x 1
   word    
   <chr>   
 1 Jason   
 2 Oscar   
 3 Maleeka 
 4 Janet   
 5 Gabriel 
 6 Raheema 
 7 Bryce   
 8 Nasreen 
 9 Hishaam 
10 Thadduse
  • desired output:
word 
Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
.
.
.

Question

1 How do I pivot word so that each new row contains 10 words per row separated by "_" ? (Tidyverse and stringr approaches would be much appreciated - thanks!)

  • Data:
> dput(df)
structure(list(word = c("Jason", "Oscar", "Maleeka", "Janet", 
"Gabriel", "Raheema", "Bryce", "Nasreen", "Hishaam", "Thadduse", 
"Marcos", "Daijah", "Chassity", "Carlito", "Chidiebere", "Matthew", 
"Maureene", "Jillian", "Markus", "Aaron", "Ramziyya", "Marquez", 
"Kiera", "Farajallah", "Larisa", "Davier", "Shujaa", "Vincent", 
"Orlando", "Joseph", "Desean", "Chelsea", "Faadil", "Christopher", 
"Aarifa", "Joel", "Matthew", "Jacob", "Aeones", "Matthew", "Jacob", 
"Savannah", "Nadia", "Kaleem", "Tanner", "Sabeeha", "Caitlyn", 
"Taylor", "Sydney", "Devin")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -50L))

答案1

得分: 4

以下是您要的中文翻译:
"你所描述的不完全是一个准确的数据透视,但您可以按照以下方式实现它:

library(tidyverse)

df %>%
  group_by((row_number() - 1) %/% 10) %>%
  summarise(word = paste(word, collapse = '_')) %>%
  select(word)
#> # A tibble: 5 x 1
#>   word                                                                           
#>   <chr>                                                                          
#> 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
#> 2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
#> 3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
#> 4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
#> 5 Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin 

"

英文:

What you are describing isn't a pivot exactly, but you can achieve it as follows:

library(tidyverse)

df %&gt;%
  group_by((row_number() - 1) %/% 10) %&gt;%
  summarise(word = paste(word, collapse = &#39;_&#39;)) %&gt;%
  select(word)
#&gt; # A tibble: 5 x 1
#&gt;   word                                                                           
#&gt;   &lt;chr&gt;                                                                          
#&gt; 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
#&gt; 2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
#&gt; 3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
#&gt; 4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
#&gt; 5 Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin 

答案2

得分: 4

这是一个基于 R 语言的代码示例,使用了矩阵方法来创建数据框。代码部分不需要翻译,以下是翻译的内容:

这是一个基于 R 语言的选项,使用了 `matrix` 方法来创建数据框
这会得到以下结果:
                                                                             word
1        Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3   Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph
4      Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew
5          Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin

请注意,这只是代码和结果的描述,不包括实际代码或数据框的内容。

英文:

Here is a base R option with matrix approach

data.frame(
    word =
        do.call(
            paste,
            c(as.data.frame(matrix(unlist(df), ncol = 10, byrow = TRUE)),
                sep = &quot;_&quot;
            )
        )
)

which gives

                                                                             word
1        Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3   Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph
4      Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew
5          Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin

答案3

得分: 3

以下是您提供的代码的中文翻译部分:

使用 tidyverse 进行实际数据透视的另一种方法。

library(tidyverse)

df %>% 
  mutate(rn = rep(1:10, nrow(.) %/% 10),
         group = rep(1:5, each = nrow(.) %/% 5)) %>% 
  pivot_wider(names_from = rn, values_from = word) %>% 
  select(-group) %>% 
  unite(col = word, sep = "_")

以下修改后的版本虽然不太美观,但更具动态性(即您只需将值更改为 word_number,无需修改管道操作)。

word_number <- 10
set_number <- nrow(df) %/% word_number

df %>% 
  mutate(rn = rep(1:word_number, set_number),
         group = rep(1:set_number, each = nrow(.) %/% set_number)) %>% 
  pivot_wider(names_from = rn, values_from = word) %>% 
  select(-group) %>% 
  unite(col = word, sep = "_")

输出

# A tibble: 5 × 1
  word                                                                           
1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
5 Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin  

希望这对您有帮助。如果您需要进一步的翻译或解释,请告诉我。

英文:

Another way to do it with tidyverse, with real pivoting.

library(tidyverse)

df %&gt;% 
  mutate(rn = rep(1:10, nrow(.) %/% 10),
         group = rep(1:5, each = nrow(.) %/% 5)) %&gt;% 
  pivot_wider(names_from = rn, values_from = word) %&gt;% 
  select(-group) %&gt;% 
  unite(col = word, sep = &quot;_&quot;)

<hr>

The following modified version is ugly, but is more dynamic (i.e. you only need to change the value to word_number without the need to modify the piped operation).

word_number &lt;- 10
set_number &lt;- nrow(df) %/% word_number

df %&gt;% 
  mutate(rn = rep(1:word_number, set_number),
         group = rep(1:set_number, each = nrow(.) %/% set_number)) %&gt;% 
  pivot_wider(names_from = rn, values_from = word) %&gt;% 
  select(-group) %&gt;% 
  unite(col = word, sep = &quot;_&quot;)

<hr>

Output

# A tibble: 5 &#215; 1
  word                                                                           
  &lt;chr&gt;                                                                          
1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
5 Jacob_Savannah_Nadia_Kaleem_Tanner_Sabeeha_Caitlyn_Taylor_Sydney_Devin  

huangapple
  • 本文由 发表于 2023年5月11日 20:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定