将案例转换为 R 中的变量
我有一个格式为 data_format1 的数据。基于此,我想将案例转换为变量以获得data_format2。
你可以在这里找到 data_format1:
ArticleID<-c(1, 2, 3, 3, 4)
Word<-c("a", "b", "b", "c", "c")
Freq<-c(2, 4, 6, 3, 2)
data_format1<-cbind(ArticleID, Word, Freq)
data_format1
ArticleID Word Freq
[1,] "1" "a" "2"
[2,] "2" "b" "4"
[3,] "3" "b" "6"
[4,] "3" "c" "3"
[5,] "4" "c" "2"
data_format2 在这里给出:
ArticleID_t<-c(1, 2, 3, 4)
a<-c(2, 0, 0, 0)
b<-c(0, 4, 6, 0)
c<-c(0, 0, 3, 2)
data_format2<-cbind(ArticleID_t, a, b, c)
data_format2
ArticleID_t a b c
[1,] 1 2 0 0
[2,] 2 0 4 0
[3,] 3 0 6 3
[4,] 4 0 0 2
I have a data with the format as data_format1. Based on it, I want to convert the cases to variables to get data_format2.
you can find data_format1 here:
ArticleID<-c(1, 2, 3, 3, 4)
Word<-c("a", "b", "b", "c", "c")
Freq<-c(2, 4, 6, 3, 2)
data_format1<-cbind(ArticleID, Word, Freq)
data_format1
ArticleID Word Freq
[1,] "1" "a" "2"
[2,] "2" "b" "4"
[3,] "3" "b" "6"
[4,] "3" "c" "3"
[5,] "4" "c" "2"
data_format2 is given here:
ArticleID_t<-c(1, 2, 3, 4)
a<-c(2, 0, 0, 0)
b<-c(0, 4, 6, 0)
c<-c(0, 0, 3, 2)
data_format2<-cbind(ArticleID_t, a, b, c)
data_format2
ArticleID_t a b c
[1,] 1 2 0 0
[2,] 2 0 4 0
[3,] 3 0 6 3
[4,] 4 0 0 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个经典的重塑问题 - 将数据从高格式转换为宽格式。
reshape2
包非常适合此目的。注意:您需要首先将数据转换为
data.frame
。请记住,cbind
将获取您的数据并创建一个数组,而不是data.frame
。因此,在解决方案中,我使用 data.frame(...) 来重新创建数据。请参阅
?reshape2::dcast
了解更多信息。This is a classical reshape problem - converting data from a tall format to a wide format. The
reshape2
package is ideal for this.Note: You need to convert your data into a
data.frame
first. Remember thatcbind
will take your data and create an array, rather than adata.frame
. So in the solution I usedata.frame(...)
to recreate your data.See
?reshape2::dcast
for more information.