【问题标题】:Populate a dataframe in r with different values用不同的值填充 r 中的数据框
【发布时间】:2020-10-12 23:52:38
【问题描述】:

谁能回答这个问题? 我有一个具有特定列名Name 的数据框(数千行),我想根据列 Name 修改数据框,例如我有一个示例数据框

df1<-data.frame(Id=c(1,2,3,4,5,6,7,8,9,10), 
       Name=c('Plant_A','Plant_A','Plant_A','Plant_A','Plant_B','Plant_B','Plant_B','Plant_C','Plant_C','Plant_C'), 
       Value=c(100,100,100,100,55,55,55,90,90,90),
       stringsAsFactors=FALSE)

现在,根据Name 列,应使用数据框df2 中显示的值添加/填充新列AvailabilityStatus。具有Yes0 的行的第一个值以及相同Name 的其余值应该是No 和空`` 等等。

df2<-data.frame(Id=c(1,2,3,4,5,6,7,8,9,10), 
                Name=c('Plant_A','Plant_A','Plant_A','Plant_A','Plant_B','Plant_B','Plant_B','Plant_C','Plant_C','Plant_C'), 
                Value=c(100,100,100,100,55,55,55,90,90,90),
                Availability=c('Yes','No','No','No','Yes','No','No','Yes','No','No'),
                Status =c(0,'','','',0,'','',0,'',''),
                stringsAsFactors=FALSE)
            

我只能添加一种类型的值,例如,

df1$Availability<-'Yes'
df1$Status<-0

但不明白如何填充df1 以获得df2。谁能帮我?谢谢。

【问题讨论】:

  • 也许merge(df1,df2)

标签: r


【解决方案1】:

dplyr 管道可以通过对数据集进行分组和变异来实现。

library(dplyr)

df1 %>%
  group_by(Name) %>%
  mutate(Availability = c("Yes", rep("No", n() - 1)), 
         Status = c(0, rep("", n() - 1)))
## A tibble: 10 x 5
## Groups:   Name [3]
#      Id Name    Value Availability Status
#   <dbl> <chr>   <dbl> <chr>        <chr> 
# 1     1 Plant_A   100 Yes          "0"   
# 2     2 Plant_A   100 No           ""    
# 3     3 Plant_A   100 No           ""    
# 4     4 Plant_A   100 No           ""    
# 5     5 Plant_B    55 Yes          "0"   
# 6     6 Plant_B    55 No           ""    
# 7     7 Plant_B    55 No           ""    
# 8     8 Plant_C    90 Yes          "0"   
# 9     9 Plant_C    90 No           ""    
#10    10 Plant_C    90 No           "" 

【讨论】:

    【解决方案2】:

    您可以对来自Name 的唯一值使用匹配。 Match 将在他的第一个参数中检索每个元素的第一个幻影。

    df1$Availability = 'No'
    df1$Status = ''
    
    
    df1$Availability[match(unique(df1$Name), df1$Name)] <- 'Yes'
    df1$Status[match(unique(df1$Name), df1$Name)] <- 0
    

    【讨论】:

    【解决方案3】:

    这是一个data.table 后人解决方案:

    library(data.table)
    
    df1<-data.table(
      Id=c(1,2,3,4,5,6,7,8,9,10), 
      Name=c('Plant_A','Plant_A','Plant_A','Plant_A','Plant_B','Plant_B','Plant_B','Plant_C','Plant_C','Plant_C'), 
      Value=c(100,100,100,100,55,55,55,90,90,90))
    
    df1[, `:=` (Availability = c("Yes", rep("", .N-1)),
                Status = c(0, rep("", .N-1))),
        by="Name"]
    
    df1[]
    #>     Id    Name Value Availability Status
    #>  1:  1 Plant_A   100          Yes      0
    #>  2:  2 Plant_A   100                    
    #>  3:  3 Plant_A   100                    
    #>  4:  4 Plant_A   100                    
    #>  5:  5 Plant_B    55          Yes      0
    #>  6:  6 Plant_B    55                    
    #>  7:  7 Plant_B    55                    
    #>  8:  8 Plant_C    90          Yes      0
    #>  9:  9 Plant_C    90                    
    #> 10: 10 Plant_C    90
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-07
      • 2022-01-13
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2023-03-29
      相关资源
      最近更新 更多