简体   繁体   English

如何为每一行中的每对观察创建一个新列 - R Language

[英]How do I create a new column for every pair of observations in each row - R Language

I would like to change my tibble so that every observation gets paired with the following observation into a new row, while retaining the id.我想更改我的 tibble,以便每个观察都与以下观察配对成一个新行,同时保留 id。

x1 <- c("cook", "clean", "wash", "walk", "wish", "broom", "clean", "wash", "walk", "cook")
x2 <- c("move", "climb", "skate", "ball", "climb", "jog", "job", "skate", "ball", "climb")
x3 <- c("try", "clean", "boom", "walk", "bring", "broom", "sing", "wash", "jump", "fly")

df <- tibble(y, x1, x2, x3)```


In this situation, 

```row 1 should be: y=1, x1="cook", x2="move"
row 2 should be: y=1, x2="move", x3="try"
row 3 should be: y=2, x1="clean",  x2="climb"
row 4 should be: y=2, x2="climb", x3="clean"```

I might have to iterate the process where the number of observations "x" is roughly equal to 50.

Thank you so much for the help!


Improved Answer改进的答案

Re-reading your question, I realize you need it to work for x1, x2 ... x50 , so I've rewritten the answer to be more flexible.重新阅读您的问题,我意识到您需要它为x1, x2 ... x50 ,所以我重写了答案以使其更加灵活。 Just include x1:x50 in the pivot_longer call instead of x1:x3 .只需在pivot_longer调用中包含x1:x50而不是x1:x3

df %>% 
  pivot_longer(names_to="obs", values_to="val", x1:x3) %>% 
  group_by(y) %>% 
  mutate(x1 = val, x2 = lead(val)) %>% 
  filter(!is.na(x2)) %>% 
  select(y, x1, x2)

Output:输出:

# A tibble: 20 x 3
# Groups:   y [10]
       y x1    x2   
   <int> <chr> <chr>
 1     1 cook  move 
 2     1 move  try  
 3     2 clean climb
 4     2 climb clean
 5     3 wash  skate
 6     3 skate boom 
 7     4 walk  ball 
 8     4 ball  walk 
 9     5 wish  climb
10     5 climb bring
11     6 broom jog  
12     6 jog   broom
13     7 clean job  
14     7 job   sing 
15     8 wash  skate
16     8 skate wash 
17     9 walk  ball 
18     9 ball  jump 
19    10 cook  climb
20    10 climb fly  

Initial Answer初步答复

Is this the pattern you need?这是你需要的模式吗?

df %>%
  bind_rows(df) %>%
  arrange(y) %>%
  group_by(y) %>%
  mutate(x1_new = if_else(row_number() == 1, x1, x2),
         x2_new = if_else(row_number() == 1, x2, x3)) %>%
  select(y, x1_new, x2_new)

Output:输出:

# A tibble: 20 x 3
# Groups:   y [10]
       y x1_new x2_new
   <int> <chr>  <chr> 
 1     1 cook   move  
 2     1 move   try   
 3     2 clean  climb 
 4     2 climb  clean 
 5     3 wash   skate 
 6     3 skate  boom  
 7     4 walk   ball  
 8     4 ball   walk  
 9     5 wish   climb 
10     5 climb  bring 
11     6 broom  jog   
12     6 jog    broom 
13     7 clean  job   
14     7 job    sing  
15     8 wash   skate 
16     8 skate  wash  
17     9 walk   ball  
18     9 ball   jump  
19    10 cook   climb 
20    10 climb  fly   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建新列 - R 语言 - How do I create a new column - R Language 如何创建新列并添加特定于 R Studio 中每一行的文本? - How do I create a new column and add text that is specific to each row in R studio? 如何在 R 中创建一行,每个行元素等于其列中的值数? - How do I create a row in R with each row element equal to the number of values in its column? 我如何编写 R 代码来创建一个新列,该列返回 dataframe 中每一行的列表列中最常见的项目 - how can I write R code to create a new column returning the most frequent item of a list column for every row in the dataframe 如何创建每行具有观察值数量的输出? - How to create an output with the number of observations in each row? R - 我有一个 for 循环来识别每一行中一列的异常值 - 如何循环查看每一列? - R - I have a for loop to identify outliers in each row for one column - how to loop to look across every column? R中每30秒进行一次求和,分为多个组,每组不规则观察,在每个新行中保留元数据 - Sum every 30s in R, multiple groups, irregular observations per group, retain metadata within each new row 如何在 R data.table 中创建一个新列,其中每一行都是前一行的 function? - How to create a new column in R data table where each row is a function of previous row? 如何在 dataframe 中组合多个观察结果以在列中创建列表? - How do I combine multiple observations in a dataframe to create a list in a column? R:如何使用从一行到另一行的 select 数据的多个条件创建新列? - R: How do i create a new column using multiple conditions from a row to select data from another row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM