Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将每个季度包含交易信息的数据集重新排列为每个交易包含一列的交易数据_R_Dplyr_Reshape - Fatal编程技术网

R 将每个季度包含交易信息的数据集重新排列为每个交易包含一列的交易数据

R 将每个季度包含交易信息的数据集重新排列为每个交易包含一列的交易数据,r,dplyr,reshape,R,Dplyr,Reshape,我很难找到重新排列数据集的方法。数据集具有以下形式: 数据 a <- data.frame(Id = c("123Ba", "672Es"), FirstFlight = c("1999-10-04","1999-10-05"), EnrollmentMonth = c("1999-10","2000-10"), Q1_1999 = c(3,0), Q2_

我很难找到重新排列数据集的方法。数据集具有以下形式:

数据

a <- data.frame(Id = c("123Ba", "672Es"), 
                FirstFlight = c("1999-10-04","1999-10-05"), 
                EnrollmentMonth = c("1999-10","2000-10"), 
                Q1_1999 = c(3,0), 
                Q2_1999 = c(0,1), 
                Q3_1999 = c(0,1))

#     Id FirstFlight EnrollmentMonth Q1_1999 Q2_1999 Q3_1999 
#1 123Ba  1999-10-04         1999-10       3       0       0
#2 672Es  1999-10-05         2000-10       0       1       1
a
这样,您将为每个航班设置行,列中包含季度信息。要根据ID累积数据,只需对其进行排序


希望有帮助

这里有一个使用
splitstackshape

library(splitstackshape)
a$Quarter = apply(a, 1, function(x) toString(rep(names(x[4:6]), x[4:6])))
cSplit(setDT(a), 'Quarter', ',', 'long')[,-(4:6), with = F]

#      Id FirstFlight EnrollmentMonth Quarter
#1: 123Ba  1999-10-04         1999-10 Q1_1999
#2: 123Ba  1999-10-04         1999-10 Q1_1999
#3: 123Ba  1999-10-04         1999-10 Q1_1999
#4: 672Es  1999-10-05         2000-10 Q2_1999
#5: 672Es  1999-10-05         2000-10 Q3_1999

美好的但是我认为你可以用
row.names(a)
替换
seq_len
,可能是这样的
b=a[rep(row.names(a),a$Q1_1999+a$Q2_1999+a$Q3_1999),1:3];b$Quarter=粘贴(substr(b$FirstFlight,1,4),b$Id,sep='.')
@VeerendraGadekar非常感谢这两个输入。这几乎是我需要的,但不完全是。我认为“季度”变量中应该包含什么有点误解。它应该显示各个季度的变量名或类似的名称,可以从中提取年份和季度数。@VeerendraGadekar:请参阅第一章中的更新post@tgrueter:我已经更新了解决方案。希望对您有所帮助!
q1= a[which(a$Q1_1999!=0),] #Select data for each quarter 
q2= a[which(a$Q2_1999!=0),] 
q3= a[which(a$Q3_1999!=0),]   

q1=q1[rep(row.names(q1),q1$Q1_1999),1:4] #repeat by number of flights
q1$Quarter='Q1_1999'  # Quarter Col. 
#do same for q2 and q3.
'''
'''    
final_data=rbind(q1,q2,q3)
library(splitstackshape)
a$Quarter = apply(a, 1, function(x) toString(rep(names(x[4:6]), x[4:6])))
cSplit(setDT(a), 'Quarter', ',', 'long')[,-(4:6), with = F]

#      Id FirstFlight EnrollmentMonth Quarter
#1: 123Ba  1999-10-04         1999-10 Q1_1999
#2: 123Ba  1999-10-04         1999-10 Q1_1999
#3: 123Ba  1999-10-04         1999-10 Q1_1999
#4: 672Es  1999-10-05         2000-10 Q2_1999
#5: 672Es  1999-10-05         2000-10 Q3_1999