r - 如何在将两根柱子保持在一起的同时熔化柱子?

标签 r melt

我有这种宽格式的数据,我想将其转换为长格式

    Cond    Construct   Line    Plant   Tube_shoot  weight_shoot    Tube_root   weight_root
1   Standard            NA      NA      2           199.95          -           -
2   Cd0     IIF         43.1    1       3           51.87           4           10.39
3   Cd0     IIF         43.1    2       5           81.80           6           15.05
4   Cd0     IIF         43.1    3       7           101.56          8           16.70

我基本上想要的是将 Tube_shoot 和 Weight_shoot 放在一起,即将这两列视为熔化的一列。但因为我只能使用

id.vars=c("Cond","Construct","Line","Plant")

结果并不完全是我想要的。

到目前为止我有两个(丑陋的)解决方案:

  1. 我融化了两次,首先通过measure.vars=c("Tube_shoot", "Tube_root"),然后通过权重,最后删除结果完全错误的一半行。这对我来说不可行,因为我有不同长度的数据,而且我总是必须检查是否取出了正确的行。

  2. 我将“ pipe ”和“重量”粘贴到一个新列中,取出其他的,将它们熔化,然后再次将它们拆开。

  3. 在 Excel 中将它们一一复制。但对于数百行,我宁愿学习如何在 R 中完成它。

我确信有更好的方法。

我最终想要的是:

    Cond    Construct   Line    Plant   Tube        weight
1   Standard            NA      NA      2           199.95
2   Cd0     IIF         43.1    1       3           51.87
3   Cd0     IIF         43.1    2       5           81.80
4   Cd0     IIF         43.1    3       7           101.56
2   Cd0     IIF         43.1    1       4           10.39
3   Cd0     IIF         43.1    2       6           15.05
4   Cd0     IIF         43.1    3       8           16.70

最佳答案

你可以试试

 res <- reshape(df1, idvar=c('Cond', 'Construct', 'Line', 'Plant'),
              varying=5:8, direction='long', sep="_")

 res1 <-  res[res$weight!='-', -5]
 row.names(res1) <- NULL

 res1
 #      Cond Construct Line Plant Tube weight_shoot
 #1 Standard             NA    NA    2       199.95
 #2      Cd0       IIF 43.1     1    3        51.87
 #3      Cd0       IIF 43.1     2    5         81.8
 #4      Cd0       IIF 43.1     3    7       101.56
 #5      Cd0       IIF 43.1     1    4        10.39
 #6      Cd0       IIF 43.1     2    6        15.05
 #7      Cd0       IIF 43.1     3    8        16.70

数据

 df1 <- structure(list(Cond = c("Standard", "Cd0", "Cd0", "Cd0"), 
  Construct = c("", "IIF", "IIF", "IIF"), Line = c(NA, 43.1, 43.1, 43.1),
  Plant = c(NA, 1L, 2L, 3L), Tube_shoot = c(2L, 3L, 5L, 7L), weight_shoot = 
  c(199.95,51.87, 81.8, 101.56), Tube_root = c("-", "4", "6", "8"), 
  weight_root = c("-", "10.39", "15.05", "16.70")), .Names = c("Cond",
  "Construct", "Line", "Plant", "Tube_shoot", "weight_shoot", "Tube_root",
  "weight_root"), class = "data.frame", row.names = c("1", "2", "3", "4"))

关于r - 如何在将两根柱子保持在一起的同时熔化柱子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124501/

相关文章:

r - 在 R 中制作这个无花果

R编程: which(colnames(df) == "thecolumnname") is not working inside a function?

r - 如何使用melt命名每个变量

google-apps-script - 如何按月列名称 GoogleSheet 将数据从宽到长的形式 reshape

python-3.x - 来自 Excel 文件的 Pandas 数据框中的双 "melt"

r - 通过在 R 中聚合复杂的配对名称求和

r - 在 R 中使用 tcltk 在弹出窗口(表格小部件)中显示数据 - 为什么它会删除最后一行数据?

r - vapply 中的错误...值的长度必须为 1,但 FUN(X[[11]]) 结果长度为 0

r - 如何 reshape 数据框以生成两列值(R 编程)

r - 使用 reshape2 熔化两组列从宽到长