将数据框形状合并为一个列

时间:2012-11-16 09:53:12

标签: r dataframe

我有一个像这样的数据框

TargetID       A    B  knowngene
cg00000108 0.8644845 0.9289447 uc003chb.3
cg00000108 0.8644845 0.9289447 uc003cha.4
cg00000109 0.8810606 0.9109397 uc003fhy.3
cg00000109 0.8810606 0.9109397 uc003fhz.4
cg00000109 0.8810606 0.9109397 uc003fia.3

我的想法是改变它以获得这个新的

knowngene    Meth_info
uc003chb.3   cg00000108;0.8644845;0.9289447
uc003cha.4   cg00000108;0.8644845 0.9289447
uc003fhy.3   cg00000109;0.8810606;0.9109397
uc003fhz.4   cg00000109;0.8810606;0.9109397
uc003fia.3   cg00000109;0.8810606;0.9109397

2 个答案:

答案 0 :(得分:2)

这几乎取自duplicated.data.frame

dat <- read.table(text = "TargetID       A    B  knowngene
cg00000108 0.8644845 0.9289447 uc003chb.3
cg00000108 0.8644845 0.9289447 uc003cha.4
cg00000109 0.8810606 0.9109397 uc003fhy.3
cg00000109 0.8810606 0.9109397 uc003fhz.4
cg00000109 0.8810606 0.9109397 uc003fia.3", header = TRUE)

# Paste together the first three columns
theStrings <- do.call(paste,c(dat[,-4],sep=";"))

# create a data.frame of the results
(dat2 <- data.frame(dat[,4, drop = FALSE], Meth_info = theStrings))

#    knowngene                     theStrings
# 1 uc003chb.3 cg00000108;0.8644845;0.9289447
# 2 uc003cha.4 cg00000108;0.8644845;0.9289447
# 3 uc003fhy.3 cg00000109;0.8810606;0.9109397
# 4 uc003fhz.4 cg00000109;0.8810606;0.9109397
# 5 uc003fia.3 cg00000109;0.8810606;0.9109397

答案 1 :(得分:0)

试试这个:

df2 <- data.frame(knowngene, 
          Meth_info= paste(TargetID,";",A,";",B,sep="")
          ,stringsAsFactors=F)

在矢量上使用粘贴只需逐行粘贴项目 - soooo方便。