本文介绍了在R中安排一个复杂的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据集:>ncol(d)[1] 1680 nrow(d)[1] 12 看起来像这样:

I have a large data set:> ncol(d) [1] 1680 nrow(d) [1] 12that it looks like this:

a  b  c  e  f  g
3  2  5  1  3  6
a  b  c  d  e  g
1  7  8  4  5  8
a  c  d  e  f  h      #in this row b does not exist
5  10 4  7  5  10

我需要它看起来像这样:

And I need that it looks like this:

a  b  c  d  e  f  g  h
3  2  5  0  3  6  10 8
1  7  8  4  5  0  8  0
5  0  10 4  7  5  0  10                 #and all the other columns ...

由于我的数据真的很长,而且我需要对所有数据集进行许多此类的更正,因此很难手动进行.我想知道是否可以使用某种自动方式(例如逻辑函数或循环)来执行此操作.任何想法都欢迎问候

Since my data is really long and I have many corrections like this one to do over all the data set, it is hard to do it by hand. I would like to know if there is any way to do this using some sort of automatic way, like a logic function or a loop.Any idea is welcomeRegards

推荐答案

以下是使用 data.table 的可能方法:

Here's a possible approach using data.table:

library(data.table)
melt(
  setDT(
    setnames(
      data.table::transpose(df1),
      paste(rep(1:(nrow(df1)/2), each = 2), c("name", "value"), sep = "_"))),
  measure = patterns("name", "value"))[
    , dcast(.SD, variable ~ value1, value.var = "value2", fill = 0)]
#    variable a b  c d e f g  h
# 1:        1 3 2  5 0 1 3 6  0
# 2:        2 1 7  8 4 5 0 8  0
# 3:        3 5 0 10 4 7 5 0 10

这篇关于在R中安排一个复杂的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:33