简体   繁体   English

R:堆叠多列

[英]R : Stacking multiple columns

I'm trying to reorganize a data set I pulled online. 我正在尝试重新整理我在线提取的数据集。 The table online is formatted so that there are two tables side by side using both the same column name. 联机表的格式被设置为使得两个并排的表使用相同的列名。 (Date, Price | Date, Price). (日期,价格|日期,价格)。

>head(data)
                    Date  Price             Date.1 Price.1
    1  December 17, 2016  1,546bp December 7, 2016 1,720bp
    2  December 16, 2016  1,628bp December 6, 2016 1,638bp
    3  December 15, 2016  1,714bp December 5, 2016 1,560bp
    4  December 14, 2016  1,805bp December 4, 2016 1,511bp
    5  December 13, 2016  1,900bp December 3, 2016 1,440bp

So what I'm trying to do is stack the 2 variables on the right underneath the 2 variables on the left to make it in chronological order. 所以我想做的是将右边的2个变量堆叠在左边的2个变量下面,以按时间顺序排列。

I've tried the stack function and the reshape2 package in R and it won't work because the data isn't all numeric. 我已经尝试了R中的stack函数和reshape2包,但由于数据不是全数字的,所以它不起作用。 I've tried other methods of creating data frames of just the two date variables and then stacking them within the new data frame but it still doesn't work. 我尝试了其他方法来创建仅包含两个日期变量的数据框,然后将它们堆叠在新的数据框中,但这仍然行不通。 Regardless, below is the code I used to try to stack the variables. 无论如何,下面是我用来尝试堆叠变量的代码。

> melt(newtable12, id.vars=c('Date', 'Date.1'), variable.name='DD')
    Date                  Date.1              DD    value
    1  December 17, 2016  December 7, 2016    Price 1,546bp
    2  December 16, 2016  December 6, 2016    Price 1,628bp
    3  December 15, 2016  December 5, 2016    Price 1,714bp
    4  December 14, 2016  December 4, 2016    Price 1,805bp
    5  December 13, 2016  December 3, 2016    Price 1,900bp
    6  December 12, 2016  December 2, 2016    Price 2,000bp
    7  December 11, 2016  December 1, 2016    Price 1,926bp
    8  December 10, 2016 November 30, 2016    Price 1,834bp
    9  December 9, 2016  November 29, 2016    Price 1,746bp
   10  December 8, 2016  November 28, 2016    Price 1,771bp

Could anyone provide any insight on correctly stacking the date/date.1 and price/price.1 columns? 谁能提供有关正确堆叠date / date.1和price / price.1列的任何见解? I'm somewhat new to R if that helps. 如果有帮助,我对R还是有些陌生。 Thank you in advance. 先感谢您。

I think this should do the trick - the last line may not be necessary, as your data appears to already be in the correct order. 我认为这应该可以解决问题-由于您的数据似乎已经按照正确的顺序排列,因此最后一行可能没有必要。

data[nrow(data) + 1:nrow(data), 1:2] <- data[, 3:4]
data <- data[, 1:2]                                  # Remove excess columns

If any re-ordering is still necessary, then if the 'date' column is formatted as dates, rather than strings, you could try: 如果仍然需要重新排序,那么如果“日期”列的格式设置为日期,而不是字符串,则可以尝试:

data <- data[order(data$Date, decreasing=T), ]       # Order by date

Otherwise, if the 'date' column is made up of characters, you could adapt something like this (although it's far from a perfect solution): 否则,如果“日期”列由字符组成,则您可以改写类似的内容(尽管这不是完美的解决方案):

# Extract date information
data$month <- unlist(strsplit(data$Date, " "))[c(T,F,F)]
data$year <- unlist(strsplit(data$Date, " "))[c(F,F,T)]
data$day <- unlist(strsplit(data$Date, " "))[c(F,T,F)]
data$month <- factor(data$month, levels=month.name)
data$day <- sapply(data$day, function(x) substr(x, 1, nchar(x)-1))

# Order the dataframe
data <- data[order(year, month, day, decreasing=TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM