将案例转换为 R 中的变量

发布于 2024-12-11 07:29:23 字数 742 浏览 0 评论 0原文

我有一个格式为 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2024-12-18 07:29:23

这是一个经典的重塑问题 - 将数据从高格式转换为宽格式。 reshape2 包非常适合此目的。

注意:您需要首先将数据转换为data.frame。请记住,cbind 将获取您的数据并创建一个数组,而不是 data.frame。因此,在解决方案中,我使用 data.frame(...) 来重新创建数据。

library(reshape2)
data_format1 <- data.frame(ArticleID, Word, Freq)
reshape2::dcast(data_format1, ArticleID~Word, sum)

  ArticleID a b c
1         1 2 0 0
2         2 0 4 0
3         3 0 6 3
4         4 0 0 2

请参阅 ?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 that cbind will take your data and create an array, rather than a data.frame. So in the solution I use data.frame(...) to recreate your data.

library(reshape2)
data_format1 <- data.frame(ArticleID, Word, Freq)
reshape2::dcast(data_format1, ArticleID~Word, sum)

  ArticleID a b c
1         1 2 0 0
2         2 0 4 0
3         3 0 6 3
4         4 0 0 2

See ?reshape2::dcast for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文