はあなたのために、この問題を引き起こして、列名が重複しています。より良い方法は、それらを分割して列名を修正してから、rbind
を使用してそれらを一緒にバインドすることです。 私はの情報を保存するために、2つの新しい列を作成することによって、すべての情報を保持しようとしたq3_...
do.call('rbind', lapply(seq(3, 12, by = 3), function(x) { y <- df1[,(x-2):x ];
y <- do.call("cbind", list(mo = colnames(y)[1], yr = colnames(y)[2], y));
colnames(y)[3:4] <- c('mo_val', 'yr_val');
y }))
# mo yr mo_val yr_val Treatment
# 1: q3_1mo q3_1yr NA NA anti-androgen
# 2: q3_1mo q3_1yr 5 2012 anti-androgen
# 3: q3_1mo q3_1yr 4 2008 anti-androgen
# 4: q3_1mo q3_1yr 4 2010 anti-androgen
# 5: q3_1mo q3_1yr NA NA anti-androgen
# 6: q3_1mo q3_1yr 2 2008 anti-androgen
# 7: q3_2mo q3_2yr 8 2010 docetaxel
# 8: q3_2mo q3_2yr 5 2012 docetaxel
# 9: q3_2mo q3_2yr 4 2008 docetaxel
# 10: q3_2mo q3_2yr 4 2010 docetaxel
# 11: q3_2mo q3_2yr 8 2011 docetaxel
# 12: q3_2mo q3_2yr 2 2008 docetaxel
# 13: q3_3mo q3_3yr NA NA abiraterone
# 14: q3_3mo q3_3yr 5 2012 abiraterone
# 15: q3_3mo q3_3yr 4 2008 abiraterone
# 16: q3_3mo q3_3yr 4 2010 abiraterone
# 17: q3_3mo q3_3yr 8 2011 abiraterone
# 18: q3_3mo q3_3yr 2 2008 abiraterone
# 19: q3_3mo q3_3yr NA NA other
# 20: q3_3mo q3_3yr 5 2012 other
# 21: q3_3mo q3_3yr 4 2008 other
# 22: q3_3mo q3_3yr 4 2010 other
# 23: q3_3mo q3_3yr 8 2011 other
# 24: q3_3mo q3_3yr 2 2008 other
# mo yr mo_val yr_val Treatment
データ:
df1 <- structure(list(q3_1mo = c(NA, 5L, 4L, 4L, NA, 2L),
q3_1yr = c(NA, 2012L, 2008L, 2010L, NA, 2008L),
Treatment = c("anti-androgen", "anti-androgen", "anti-androgen", "anti-androgen", "anti-androgen", "anti-androgen"),
q3_2mo = c(8L, 5L, 4L, 4L, 8L, 2L),
q3_2yr = c(2010L, 2012L, 2008L, 2010L, 2011L, 2008L),
Treatment = c("docetaxel", "docetaxel", "docetaxel", "docetaxel", "docetaxel", "docetaxel"),
q3_3mo = c(NA, 5L, 4L, 4L, 8L, 2L),
q3_3yr = c(NA, 2012L, 2008L, 2010L, 2011L, 2008L),
Treatment = c("abiraterone", "abiraterone", "abiraterone", "abiraterone", "abiraterone", "abiraterone"),
q3_3mo = c(NA, 5L, 4L, 4L, 8L, 2L),
q3_3yr = c(NA, 2012L, 2008L, 2010L, 2011L, 2008L),
Treatment = c("other", "other", "other", "other", "other", "other")),
.Names = c("q3_1mo", "q3_1yr", "Treatment", "q3_2mo", "q3_2yr", "Treatment", "q3_3mo", "q3_3yr", "Treatment", "q3_3mo", "q3_3yr", "Treatment"),
row.names = c(NA, -6L), class = "data.frame")
ユニークでない一意の名前を持つデータフレーム(またはリストのような名前付きオブジェクト)は真剣に間違っています(df [["Treatment"]]は何を参照していますか?)最初は建物を建てることは避けるべきです。 –