我正在尝试将一列添加到数据框的末尾。该列应该填充列表id
。但是,当我写w <-cbind(w, id)
时,它会将整个id
打印到数据框的每个单元格中。
当前输出:
optOne% optTwo% Neither% TotalCount id
1 20 1 1 21 A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
2 17 0 1 21 A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
3 15 2 4 21 A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
4 12 0 9 21 A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
5 18 1 2 21 A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
期望的输出:
optOne% optTwo% Neither% TotalCount id
1 20 1 1 21 A1779TGH2ZMWJ2
2 17 0 1 21 A18ACKRAVMM8D8
3 15 2 4 21 A19JXVM2WO1CJ8
4 12 0 9 21 A1A7O3TW2O0TPM
5 18 1 2 21 A1AKB300WFZPZ3
答案 0 :(得分:1)
看起来id
是一个字符串,因此您首先需要strsplit
(输出一个列表)。然后使用$
w$id <- unlist(strsplit(id,split=" "))
答案 1 :(得分:0)
你也可以这样做:
dat <- read.table(text="optOne%,optTwo%,Neither%,TotalCount,id
1,20,1,1,21,A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
2,17,0,1,21,A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
3,15,2,4,21,A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
4,12,0,9,21,A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3
5,18,1,2,21,A1779TGH2ZMWJ2 A18ACKRAVMM8D8 A19JXVM2WO1CJ8 A1A7O3TW2O0TPM A1AKB300WFZPZ3",
sep=",",header=TRUE,stringsAsFactors=F,check.names=F)
transform(dat, id=unique(scan(text=id, what="character")))
#Read 25 items
# optOne. optTwo. Neither. TotalCount id
#1 20 1 1 21 A1779TGH2ZMWJ2
#2 17 0 1 21 A18ACKRAVMM8D8
#3 15 2 4 21 A19JXVM2WO1CJ8
# 4 12 0 9 21 A1A7O3TW2O0TPM
#5 18 1 2 21 A1AKB300WFZPZ3