首页 文章

将行内容转置为一列,然后对下一行执行相同操作

提问于
浏览
0

我有一个名为 full_data_string_split_removed2dataframe . 当我这样做 SP <- which(full_data_string_split_removed2$split1 == "SP#") 然后我得到它找到表达式 SP# 的行号 . 做 print(full_data_string_split_removed2) 给出:data

在这种情况下,执行: Number_of_SP_lines <- length(SP)print(Number_of_SP_lines) 给出 [1] 425 是正确的 . 首先,常量是我有一个行,其中表达式 SP# 可以在列 split1 中找到,第二个常量是它后跟103行数据,如我的示例数据中所示 . 但是,不同数据集的 SP# 出现次数可能不同 . 所以我需要实现的是:

  • 在列 split1 中找到 SP# 的行 split7 中的条目并将该值除以 60 并复制到新的表格单元格A2中,其中A1将从其满足条件的行中获取列 samplerepetition 的名称列 split1 中的 SP# .

  • 然后将以下103行的 split2 列中的条目转换为 split11 到项目符号点1条目下的新数据框/表中,这些条目为1024个条目 .

  • 对剩余的 SP# 事件执行步骤1和2,而每个 SP# 事件应该获得自己的列 .

1 回答

  • 2

    以下代码应该做你想要的:

    # Read in the data
    tbl1 <- read.csv('SP21_only.csv')
    # Find the rows where SP# is in split1
    SP_indices <- which(grepl('SP#', tbl1$split1))
    # Then store in tbl2, for each SP_indices row
    tbl2 <- sapply(SP_indices, function(i){
        # That observation of sample + that observation of repetition
        c(paste(tbl1$sample[i], tbl1$repetition[i]),
          # That observation of split7 / 60
          tbl1$split7[i] / 60,
          # And concatenation into a vector the transposition of the next
          # 103 rows for the columns split2-split11
          c(t(tbl1[i + 1:103, paste0('split', 2:11)])))
    })
    

    请注意,结果矩阵的尺寸将为1032行和425列,如上面的评论中所述 . 这适用于任意数量的 SP# 次出现,但只有在 SP# 次出现之间总共有103行时才有效 . 如果您需要它来处理任意数量的插入行,您可以执行以下操作:

    # Read in the data
    tbl1 <- read.csv('SP21_only.csv')
    # It will be convenient to go ahead and paste together sample and repitition
    sample_repetition <- paste(tbl1$sample, tbl1$repetition)
    # Then we get a vector of length nrow(tbl1)
    # that increments in value everytime split1 contains SP#
    # This groups or separates the data into segments we need
    groups <- cumsum(grepl('SP#', tbl1$split1))
    # Then store in tbl2, for each group
    tbl2 <- sapply(1:max(groups), function(x){
        group_indices <- which(groups == x)
        first_index <- min(group_indices)
        # The relevant element of sample_repetition,
        # The relevant element of split7 / 60, and
        return(c(sample_repetition[first_index], tbl1$split7[first_index] / 60,
                 # the concatenation of the transposition of the relevant submatrix
                 c(t(tbl1[group_indices[-1], paste0('split', 2:11)]))))
    })
    

相关问题