【问题标题】:Changing the columns to rows with rbind使用 rbind 将列更改为行
【发布时间】:2013-04-06 19:22:57
【问题描述】:

我有一个数据集如下:

     N2     N3     N4    N5     N7     N8    N10    N12   N13    N14   N17   N19    N25    N28   N29    N31    N32
1 1.300 -0.170 -0.344 2.138  0.684  0.656  0.882  0.684 1.822  1.214 2.046 2.432  0.208  0.312 0.530  0.358  0.264
2 0.888 -0.534 -0.684 1.442 -0.178 -0.060  0.430 -0.148 1.420  0.286 1.444 2.138 -0.264 -0.042 0.398 -0.196 -0.148
3 0.792 -0.564 -0.622 0.998 -0.320  1.858 -0.036 -0.118 1.476  0.110 0.964 2.048 -0.480 -0.434 0.040 -0.538 -0.322
4 0.324 -1.022 -1.128 1.380 -0.792  1.042 -0.054 -0.158 1.518 -0.102 1.354 2.386 -0.708 -0.510 0.258 -0.696 -0.566
5 0.650 -0.774 -0.982 1.124 -0.540  3.200 -0.052 -0.258 1.452  0.028 1.022 2.110 -0.714 -0.646 0.266 -0.768 -0.532
6 0.670 -0.660 -0.844 1.248 -0.550  2.868 -0.098 -0.240 1.380 -0.012 1.164 2.324 -0.498 -0.474 0.860 -0.588 -0.324
  MeteoSwiss
1       -0.6
2       -1.2
3       -1.0
4       -0.8
5       -0.4
6       -0.2

我想更改行和列的位置。

我使用 rbind 如下:

data <- data.frame(hour1=numeric(0),hour2=numeric(0),hour3=numeric(0),hour4=numeric(0),hour5=numeric(0),hour6=numeric(0),hour7=numeric(0),hour8=numeric(0),hour9=numeric(0),hour10=numeric(0),hour11=numeric(0),hour12=numeric(0),hour13=numeric(0),hour14=numeric(0),hour15=numeric(0),hour16=numeric(0),hour17=numeric(0),hour18=numeric(0),hour19=numeric(0),hour20=numeric(0),hour21=numeric(0),hour22=numeric(0),hour23=numeric(0),hour24=numeric(0))


data <- rbind(data[1,],data2)
data <- rbind(data[2,],data3)
data <- rbind(data[3,],data4)
data <- rbind(data[4,],data5)
data <- rbind(data[5,],data7)
data <- rbind(data[6,],data8)
data <- rbind(data[7,],data10)
data <- rbind(data[8,],data12)
data <- rbind(data[9,],data13)
data <- rbind(data[10,],data14)
data <- rbind(data[11,],data17)
data <- rbind(data[12,],data19)
data <- rbind(data[13,],data25)
data <- rbind(data[14,],data28)
data <- rbind(data[15,],data29)
data <- rbind(data[16,],data31)
data <- rbind(data[17,],data32)
data <- rbind(data[18,],sw)

问题是显然 rbind 不能用于超过 2 行。

【问题讨论】:

  • 你的代码没有意义。什么是data2data3、...?顺便说一句,你考虑过t(data) 吗?
  • @CarlWitthoft: data2=N2, data3=N3, ... 是列数据源。
  • 第一个数据框与您使用 rbind 所做的事情有什么关系?您是否还说您有大量数据框并且想要获取它们的列并将它们作为行绑定在一起? rbind 也可以处理超过 2 行:rbind(mtcars[, 1], mtcars[, 2], mtcars[, 4])
  • @TylerRinker 数据是第一个数据框的格式,但我在单独的表中也有第一个数据框的列。
  • 您是否正在寻找 reshape2-package 中的功能?

标签: r multiple-columns


【解决方案1】:

您可能正在寻找:

 library(reshape2)
 melt(data)

【讨论】:

    【解决方案2】:

    使用 reshape 可能有更好的方法来做到这一点,但这是我目前能想到的

    ## df is your data frame I am creating a dummy one here...
    df <- data.frame(N1 = runif(5), N2 = runif(5), N3 = runif(5), N4 = runif(5))
    
    ## then we create a list of single column data frames...
    dfList <- lapply(1:ncol(df), function(x) data.frame(col = df[[x]]))
    
    ## then we bind them all using do.call and rbind
    df <- do.call(rbind, dfList)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2019-01-14
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多