请帮助我,我有这个数据框:
df <- data.frame(G1=c("a","b","c","d","e"),
G2=c("a","c","b","d","e"),
G3=c("a","d","e","b","c"),
G4=c("a","b","c","d","e"))
我想制作另一张表格,显示从G1到G4的每个术语的位置,所以它看起来像这样:
rank <- data.frame(term=c("a","b","c","d","e"),
G1=c(1:5),
G2=c(1,3,2,4,5),
G3=c(1,4,5,2,3),
G4=c(1:3,4,5))
我的第二个问题:是否有一种图形化方式可以将表格中的每个元素统一起来,例如,“b”的箭头,从[2,1]到[3,2]到[4,3]到[ 2,4)
谢谢!
编辑:
我已在原始数据框中应用了这些建议。这是一个例子:
df <- data.frame(G1=c("bta-let-7a-1","bta-let-7b", "bta-mir-26a-1", "bta-mir-21"),
G2=c("bta-mir-21", "bta-let-7a-1", "bta-let-7b", "bta-mir-26a-1"),
G3=c("bta-mir-26a-1", "bta-mir-21", "bta-let-7a-1", "bta-let-7b"),
G4=c("bta-let-7b","bta-mir-26a-1", "bta-mir-21", "bta-let-7a-1"))
问题是,当我lapply
时,“G1”列变为“1”,“2”,“4”,“3”。
rank <- as.data.frame(lapply(df, as.integer))
但是,“G1”为“1”,“2”,“3”,“4”,其余列根据“G1”排列。例如,“G2”是“4”,“1”,“2”,“3”。 (这在我的原始表中变得更加混乱)
答案 0 :(得分:0)
由于你已经有了因素,所以很容易做到:
as.data.frame(lapply(df, as.integer))
# G1 G2 G3 G4
#1 1 1 1 1
#2 2 3 4 2
#3 3 2 5 3
#4 4 4 2 4
#5 5 5 3 5
如果你想像编辑过的OP一样重新排序你的因素,一种方法是:
as.data.frame(lapply(lapply(df, function(x)
factor(as.character(x), as.character(df$G1))), as.integer))