我正在尝试将数据从宽格式重塑为长格式。在下表中,我有:
Sample 1 Sample 2 Sample 3 ... Sample 18
string1 string2 0 String3
0 string1 0 0
0 0 0 0
如您所见,几个样本可以具有相同的字符串。样本是colnames。我想将以下内容放入向量中。我不想要任何零,我需要每个字符串的所有实例:
string1
string2
string1
string3
到目前为止,我编写了以下代码:
reshape(SV37.refined, direction="long",varying=names(SV37.refined), v.names="Value", idvar ="Index", times=names(SV37.refined), timevar="Sample")
SV37.refined是我的数据框的名称。但是,我得到了:
1.Sample1 Sample1 string1 1
2.Sample1 Sample1 0 2
3.Sample1 Sample1 0 3
4.Sample2 Sample2 string2 4
5.Sample2 Sample2 string1 5
6.Sample2 Sample2 0 6
你知道吗?
非常感谢你的时间!
答案 0 :(得分:1)
如果没有必要使用reshape
out <- unlist(lapply(SV37.refined, as.character))
out[out != "0"]
## Sample11 Sample21 Sample22 Sample181
## "string1" "string2" "string1" "string3"
或者如果您进入单行
Filter(function(x) x != "0", unlist(lapply(SV37.refined, as.character)))
## Sample11 Sample21 Sample22 Sample181
## "string1" "string2" "string1" "string3"
答案 1 :(得分:1)
使用reshape
:
dat <- read.table(text="
Sample1 Sample2
string1 string2
0 string1
0 0", header=TRUE)
# Sample1 Sample2
#1 string1 string2
#2 0 string1
#3 0 0
out <- reshape(
dat,
varying=c("Sample1","Sample2"),
direction="long",
times=1:2,
v.names="Value",
timevar="Sample"
)
out[out$Value != 0,]
# Sample Value id
#1.1 1 string1 1
#1.2 2 string2 1
#2.2 2 string1 2