首页 > 解决方案 > 读取组中的特定位置以在列 R 中创建新行

问题描述

我有多个具有双列结构的 csv 文件。一旦文件在 R 中,它看起来像这样

# A tibble: 18 x 3
# Groups:   group [2]
   V1                        V2                                          group
   <chr>                     <chr>                                       <int>
 1 Sample File               "C:\\Data\\CPC\\COALA_CPC3776_20200129.xls"     0
 2 Model                     "3776"                                          0
 3 Sample #                  "1"                                             1
 4 Start Date                "01/29/20"                                      1
 5 Start Time                "03:06:08"                                      1
 6 Sample Length             "04:58"                                         1
 7 Averaging Interval (secs) "1.0"                                           1
 8 Title                     ""                                              1
 9 Instrument ID             "3776  70634317 2.7"                            1
10 Instrument Errors         "None"                                          1
11 Mean                      "4687.93"                                       1
12 Min                       "4215"                                          1
13 Max                       "5095"                                          1
14 Std. Dev.                 "208.445"                                       1
15 Time                      "Concentration (#/cm³)"                         1
16 03:06:09                  "4581"                                          1
17 03:06:10                  "4673"                                          1
18 03:06:11                  "4657"                                          1

这种格式每 5 分钟重复一次。我想将日期和样本 # 移动到新列,然后删除样本文件到 Std.Dev 之间的所有其他行。在 V1 中得到这样的东西。

   time concentration     date sample
1 02:02:02          1200 01/01/01      2
2 02:02:03          1300 01/01/01      2
3 02:03:03          4000 01/01/01      2

我可以按样本 # 对数据进行分组,但我不知道如何继续。到目前为止,这是我的代码

cpc_files <- list.files(pattern = '*.xls',path = 'input/CPC/')

cpc_raw <- do.call("rbind",  ##Apply the bind to the files
        lapply(cpc_files, ##call the list
               function(x)  ##apply the next function
                 read.table(paste("input/CPC/", x, sep=''),sep=',',fill = T, header = F, 
                          stringsAsFactors = FALSE,comment.char = "",
                          col.names = paste0("V",seq_len(max(count.fields("input/CPC/COALA_CPC3776_20200129.xls", sep = ','))))))) ##Read all the files filling the blanks with NAs

cpc_fix <- cpc_raw%>%select(V1,V2)%>%
          group_by(group = cumsum(V1 == "Sample #"))

标签: r

解决方案


我将您的输入简化为 2 列,但这应该是一个好的开始。

x <- read.csv(file = '~/file.csv', stringsAsFactors = F)

df <- cbind(t(x$V2[1:(which('Time'==x$V1)-1)]), 
           x[(which('Time'==x$V1)+1):nrow(x),], stringsAsFactors = F)

colnames(df) <- unlist(c(x$V1[1:(which('Time'==x$V1)-1)], 
                                 x[(which('Time'==x$V1)),]))

的第一个参数cbind是元数据(第 1 行到它找到的位置'Time'),第二个参数是样本(之后的所有内容'Time')。设置列名的逻辑相同。如果需要,您还可以将名称存储为一行。

df2 <- rbind(colnames(df), df)

推荐阅读