R重新排列数据框:一些行到列 [英] R re-arrange dataframe: some rows to columns
问题描述
我什至不知道如何正确地为问题命名!
I'm not even sure how to title the question properly!
假设我有一个数据框 d:
Suppose I have a dataframe d:
当前数据框:
d <- data.frame(sample = LETTERS[1:2], cat = letters[11:20], count = c(1:10))
sample cat count
1 A k 1
2 B l 2
3 A m 3
4 B n 4
5 A o 5
6 B p 6
7 A q 7
8 B r 8
9 A s 9
10 B t 10
并且我正在尝试重新排列事物,使每个 cat 值成为它自己的一列,sample 仍然是一列(或成为行名称),而 count 将是新 cat 列中的值,其中0,其中样本没有猫的数量.像这样:
and I'm trying to re-arrange things such that each cat value becomes a column of its own, sample remains a column (or becomes the row name), and count will be the values in the new cat columns, with 0 where a sample doesn't have a count for a cat. Like so:
所需的数据框布局:
sample k l m n o p q r s t
1 A 1 0 3 0 5 0 7 0 9 0
2 B 0 2 0 4 0 6 0 8 0 10
解决这个问题的最佳方法是什么?
What's the best way to go about this?
这是我得到的:
for (i in unique(d$sample)) {
s <- d[d$sample==i,]
st <- as.data.frame(t(s[,3]))
colnames(st) <- s$cat
rownames(st) <- i
}
即循环遍历原始数据帧中的样本,并对每个样本子集进行转置.所以在这种情况下我得到
i.e. looping through the samples in the original data frame, and transposing for each sample subset. So in this case I get
k m o q s
A 1 3 5 7 9
和
l n p r t
B 2 4 6 8 10
这就是我卡住的地方.我用 merge
、bind
、apply
尝试了很多东西,但我似乎无法正确命中事物.另外,我不禁想知道上面的循环是否是一个必要的步骤——也许是unstack
?
And this is where I get stuck. I've tried a bunch of things with merge
, bind
, apply
,... but I can't seem to hit on the right thing. Plus, I can't help but wonder if that loop above is a necessary step at all - something with unstack
perhaps?
不用说,我是 R 的新手...如果有人能帮助我,将不胜感激!
Needless to say, I'm new to R... If someone can help me out, it would be greatly appreciated!
PS 原因我试图重新排列我的数据框是为了让绘制值更容易(即我想以表格格式在图中显示实际的 df).
PS Reason I'm trying to re-arrange my dataframe is in the hopes of making plotting of the values easier (i.e. I want to show the actual df in a plot in table format).
谢谢!
推荐答案
Using reshape
from base R:
Using reshape
from base R:
nn<-reshape(d,timevar="cat",idvar="sample",direction="wide")
names(nn)[-1]<-as.character(d$cat)
nn[is.na(nn)]<-0
> nn
sample k l m n o p q r s t
1 A 1 0 3 0 5 0 7 0 9 0
2 B 0 2 0 4 0 6 0 8 0 10
这篇关于R重新排列数据框:一些行到列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!