简体   繁体   English

重新排列数据框中的值并将其提取到 R 中的特定列

[英]Rearrange and extract values from a data frame to specific columns in R

I have a dataframe df like this我有一个像这样的 dataframe df

> df <- data.frame(type=c("Id","v1","v2","Id","v1","v1","v2","Id","v1","v2","v3"),num=c(1000,200,500,1001,727,50,800,1002,400,365,865))
> df
   type  num
1    Id 1000
2    v1  200
3    v2  500
4    Id 1001
5    v1  727
6    v1   50
7    v2  800
8    Id 1002
9    v1  400
10   v2  365
11   v3  865

I need to create another data frame with Id, v1, v2, v3 as the column names and corresponding values from df such that the variable below each id belong to that id and when the same variable repeats it has to be mapped with the same id and if the variable is not present NA has to be given.我需要创建另一个数据框,其中Id、v1、v2、v3作为列名和df中的相应值,以便每个 id 下面的变量属于该 id,并且当相同的变量重复时,它必须映射为相同的 id如果变量不存在,则必须给出 NA。 This is the desired output.这是所需的 output。

    Id  v1  v2  v3
1 1000 200 500  NA
2 1001 727  NA  NA
3 1001  50 800  NA
4 1002 400 365 865

I have thought about a method using for loop.我想过一种使用 for 循环的方法。 But it seems complicated and difficult to structure it.但它似乎复杂且难以构建它。 Is there a way without using for loops.有没有办法不使用 for 循环。

try to do it this way尝试这样做

   library(tidyverse) 
   df %>% 
      mutate(id = ifelse(type == "Id", num, NA)) %>% 
      fill(id) %>% 
      filter(type != "Id") %>% 
      group_by(id, type) %>% 
      mutate(n = row_number()) %>% 
      pivot_wider(c(id, n), names_from = type, values_from = num) %>% 
      select(-n) %>% 
      ungroup()

# A tibble: 4 x 4
     id    v1    v2    v3
  <dbl> <dbl> <dbl> <dbl>
1  1000   200   500    NA
2  1001   727   800    NA
3  1001    50    NA    NA
4  1002   400   365   865

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM