在R中生成一个向量,并将其插入到一个堆叠的框架中[英] generate a vector in R and insert it in a stacked frame

本文是小编为大家收集整理的关于在R中生成一个向量,并将其插入到一个堆叠的框架中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述


我想堆叠一个框架,但我认为我不使用正确的方法...

myframe
 sex = F

     q8
cars   N   U   Y
       1  35  31  10
       2  34  23   7
       3 132 109  35
       4  38  36  14
       5   7   5   2

, , sex = M

     q8
cars   N   U   Y
       1  49  22  16
       2  24  13   8
       3 136  52  33
       4  37  31  32
       5  15  10   4

f = c(myframe[1:15])
m = c(myframe[16:30])

S <- stack(data.frame(f,m))


names(S)=c("num","gender")
group=c("1","1","1","2","2","2","3","3","3","4","4","4","5","5","5","6","6","6","1","1","1","2","2","2","3","3","3","4","4","4","5","5","5")

num=S$num
gender=S$gender
twoway.df=data.frame(num,group,gender)
twoway.df
1   1 35 f
2   1 31 f
3   1 10 f
4   2 24 f
5   2 13 f
      ....

你能帮我做得更好吗?

推荐答案

要将表转换为data.frame,基本函数as.data.frame.table应该有效.

但是,我将如何做:

myframe <- as.table(array(c(35, 34, 132, 38, 7, 31, 23, 109, 36, 5, 
                            10, 7, 35, 14, 2, 49, 24, 136, 37, 15, 
                            22, 13, 52, 31, 10, 16, 8, 33, 32, 4), 
                            dim=c(5, 3, 2), 
                            dimnames=list(cars=1:5, q8=c("N","U","Y"), 
                                          sex=c("F","M"))))
library(reshape)
melt(myframe)

获取所有变量的data.frame.您是否应该只想将q8和sex作为数据中的因素.FRAME,请使用melt(myframe)[,-1].

有关更多信息,请参见help(melt.array).

本文地址:https://www.itbaoku.cn/post/2091175.html

问题描述


I want to stack a frame but I think I do not use the right way...

myframe
 sex = F

     q8
cars   N   U   Y
       1  35  31  10
       2  34  23   7
       3 132 109  35
       4  38  36  14
       5   7   5   2

, , sex = M

     q8
cars   N   U   Y
       1  49  22  16
       2  24  13   8
       3 136  52  33
       4  37  31  32
       5  15  10   4

f = c(myframe[1:15])
m = c(myframe[16:30])

S <- stack(data.frame(f,m))


names(S)=c("num","gender")
group=c("1","1","1","2","2","2","3","3","3","4","4","4","5","5","5","6","6","6","1","1","1","2","2","2","3","3","3","4","4","4","5","5","5")

num=S$num
gender=S$gender
twoway.df=data.frame(num,group,gender)
twoway.df
1   1 35 f
2   1 31 f
3   1 10 f
4   2 24 f
5   2 13 f
      ....

could you help me do it better ?

推荐答案

To transform a table into a data.frame, the base function as.data.frame.table should work.

Here is, however, how I would do:

myframe <- as.table(array(c(35, 34, 132, 38, 7, 31, 23, 109, 36, 5, 
                            10, 7, 35, 14, 2, 49, 24, 136, 37, 15, 
                            22, 13, 52, 31, 10, 16, 8, 33, 32, 4), 
                            dim=c(5, 3, 2), 
                            dimnames=list(cars=1:5, q8=c("N","U","Y"), 
                                          sex=c("F","M"))))
library(reshape)
melt(myframe)

for getting a data.frame with all variables. Should you only want to keep q8 and sex as factors in your data.frame, use melt(myframe)[,-1] instead.

See help(melt.array) for more information.