【问题标题】:R write text at specific location in a dataframeR在数据框中的特定位置写入文本
【发布时间】:2020-09-07 19:03:59
【问题描述】:

我有一个数据框,其中包含超过 482 天的数据,每天我有 224 个值,这使其成为长度为 107'968 的数据框。现在我想在每天之前添加与该确切日期相对应的文本。

我知道这不能在数据框中完成,我也用列表尝试过,但我不知道应该如何解决这个问题。

我的数据框看起来像这样:

Layer    Row     Column    Shead    Ehead
1        1       1         40.1     40.4
1        1       2         40.3     40.5
1        1       3         40.2     40.6

1        1       1         40.4     40.2
1        1       2         40.5     40.3
1        1       3         40.6     40.7

1        1       1         40.2     40.4
1        1       2         40.3     40.4
1        1       3         40.7     40.8

在这里,我为一天设置了 3 个值,这超过了 3 天。正如我之前所说,真实数据超过 482 天,每天有 224 个值。但如果它适用于这些数据,那么它也应该适用于更大的数据。

现在我的目标是达到这个目标:

Layer    Row     Column    Shead    Ehead
3  0 -- 5. ITMP NP. Stress Period 1
1        1       1         40.1     40.4
1        1       2         40.3     40.5
1        1       3         40.2     40.6

3  0 -- 5. ITMP NP. Stress Period 2
1        1       1         40.4     40.2
1        1       2         40.5     40.3
1        1       3         40.6     40.7

3  0 -- 5. ITMP NP. Stress Period 3   
1        1       1         40.2     40.4
1        1       2         40.3     40.4
1        1       3         40.7     40.8

其中文本的第一个值属于每个周期/天的值数,第二个值必须为零。末尾的期间编号应每天增加。

我希望我的数据框看起来像这样,因为我希望它可以在地下水流模拟软件 (Processing Modflow X) 中读取,并且为了读取其中的值,每天之前必须有正确的文本。

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    这是一种解决方案:

    library(dplyr)
    library(tidyr)
    library(tibble)
    library(magrittr)
    
    #Data
    #Shead and Ehead are just random decimal values affixed to "40" here.
    df <- data.frame(Layer = rep_len(1, 9),
                     Row = rep_len(1, 9),
                     Column = rep(c(1:3), 3),
                     Shead = as.numeric(paste0(40, ".", seq(1:9))),
                     Ehead = as.numeric(paste0(40, ".", seq(9:1))))
    
    #How many rows we have per group
    perg <- 3
    
    #How many groups (i.e., days in your case) we have in total
    totg <- 3
    
    #We need to create some sort of grouping variable
    df$Group <- rep(1:totg, each = perg)
    
    #Adding an empty row first
    df %<>% group_by(Group) %>%
      do(add_row(., .before = 0))
    
    #Resetting the grouping variable to accommodate the extra row per group
    df$Group <- rep(1:totg, each = perg+1)
    
    #Adding the string
    df %<>% group_by(Group) %>%
      mutate(Layer = ifelse(is.na(Layer), paste0(perg, " 0 -- 5. ITMP NP. Stress Period ", Group), as.character(Layer)))
    
    #Removing the NAs
    df <- as.data.frame(df)
    df[is.na(df)] <- ""
    
    #Dropping the grouping column
    df %<>% select(-Group)
    
    df
    
    #                                 Layer Row Column Shead Ehead
    # 1  3 0 -- 5. ITMP NP. Stress Period 1                       
    # 2                                   1   1      1  40.1  40.1
    # 3                                   1   1      2  40.2  40.2
    # 4                                   1   1      3  40.3  40.3
    # 5  3 0 -- 5. ITMP NP. Stress Period 2                       
    # 6                                   1   1      1  40.4  40.4
    # 7                                   1   1      2  40.5  40.5
    # 8                                   1   1      3  40.6  40.6
    # 9  3 0 -- 5. ITMP NP. Stress Period 3                       
    # 10                                  1   1      1  40.7  40.7
    # 11                                  1   1      2  40.8  40.8
    # 12                                  1   1      3  40.9  40.9
    

    此代码的作用是首先添加包含NAs 的行,每个perg 行数。然后它分别使用pergGroup 中的值将您要求的字符串写入Layer 列。然后它会从 data.frame 中删除所有 NA。

    您必须将 perg 更改为 224 并将 totg 更改为 482 才能使用您的数据。此外,由于NAs 已被替换为空白,因此当您将数据写入文件时,不应使用空格(\s 或任何等价物)作为分隔符。

    【讨论】:

      【解决方案2】:

      为您的df 创建一个list

      list_df <- sapply(df[1:10,1],list)
      

      lapply 使用匿名函数使用 paste 和临时变量 x 来访问 list_df 的第一个值并写入 "datehere" 并存储在列表 final 中,lapply 也返回一个列表。

      list_final <- lapply(list_df, function(x) paste0("datehere",x[[1]]))
      

      【讨论】:

      • 感谢您的回答,但它对我的数据不太有效。我现在编辑了我的问题,所以如果你再看一下我会很高兴。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多