首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建嵌套的数据框

如何创建嵌套的数据框
EN

Stack Overflow用户
提问于 2011-01-31 17:53:01
回答 2查看 7.3K关注 0票数 1

我有一个看起来像这样的数据。

代码语言:javascript
复制
> file1="dat1.tab"
> file2="dat2.tab"
> dat1<-read.table(file1)
> print(dat1)
 V1 V2
1  1 43
2  1 43
3  1 43
>
> dat2<-read.table(file2)
> print(dat2)
 V1 V2
1  1 43
2  1 21
3  1 43
4  1 43
5  1 24
6  0 24
>

列V1指的是标签,V2指的是预测得分。

如何创建一个名为HIV的数据结构,如下所示:

代码语言:javascript
复制
> HIV
$hiv.dat1
$hiv.dat1$predictions
$hiv.dat1$predictions[[1]]
[1] 43 43 43
$hiv.dat$labels
$hiv.dat$labels[[1]]
[1] 1 1 1

$hiv.dat2
$hiv.dat2$predictions
$hiv.dat$predictions[[1]]
[1] 43 21 43 43 24 24
$hiv.dat2$labels
$hiv.dat2$labels[[1]]
[1] 1 1 1 1 1 0
EN

回答 2

Stack Overflow用户

发布于 2016-06-07 06:40:15

自从这个问题被提出以来,“嵌套数据框架”就具有了特定的含义。在2016年,它可能会被解释为:

代码语言:javascript
复制
dat1 <- data.frame(labels = rep(1, 3), predictions = rep(43, 3))
dat2 <- data.frame(labels = c(rep(1, 5), 0),
                   predictions = c(43, 21, 43, 43, 24, 24))
dat1
#>   labels predictions
#> 1      1          43
#> 2      1          43
#> 3      1          43
dat2
#>   labels predictions
#> 1      1          43
#> 2      1          21
#> 3      1          43
#> 4      1          43
#> 5      1          24
#> 6      0          24

dat <- list(HIV = 1:2, data = list(dat1, dat2))
attr(dat, "row.names") <- 1:2
class(dat) <- c("tbl_df", "data.frame")

dat # nested data frame, using the "tidyr" package definition of "nest"
#>   HIV                                     data
#> 1   1                      1, 1, 1, 43, 43, 43
#> 2   2 1, 1, 1, 1, 1, 0, 43, 21, 43, 43, 24, 24
str(dat)
#> Classes 'tbl_df' and 'data.frame':   2 obs. of  2 variables:
#>  $ HIV : int  1 2
#>  $ data:List of 2
#>   ..$ :'data.frame': 3 obs. of  2 variables:
#>   .. ..$ labels     : num  1 1 1
#>   .. ..$ predictions: num  43 43 43
#>   ..$ :'data.frame': 6 obs. of  2 variables:
#>   .. ..$ labels     : num  1 1 1 1 1 0
#>   .. ..$ predictions: num  43 21 43 43 24 24

library(tidyr)
dat # nicer printing with the tidyr package
#> Source: local data frame [2 x 2]
#> 
#>     HIV               data
#>   (int)              (chr)
#> 1     1 <data.frame [3,2]>
#> 2     2 <data.frame [6,2]>
unnest(dat) # ordinary data frame representation
#> Source: local data frame [9 x 3]
#> 
#>     HIV labels predictions
#>   (int)  (dbl)       (dbl)
#> 1     1      1          43
#> 2     1      1          43
#> 3     1      1          43
#> 4     2      1          43
#> 5     2      1          21
#> 6     2      1          43
#> 7     2      1          43
#> 8     2      1          24
#> 9     2      0          24
票数 4
EN

Stack Overflow用户

发布于 2011-01-31 18:13:15

你可以理清细节,但大致如下:

代码语言:javascript
复制
dat1 <- data.frame(V1 = rep(1, 5), V2 = sample(c(40:45), 5)
dat2 <- data.frame(V1 = sample(c(0,1), 5, replace = TRUE),
 V2 = sample(c(40:45), 5, replace = TRUE))

> hiv <- list(hiv.dat1 = as.list(dat1), hiv.dat2 = as.list(dat2))
> hiv
$hiv.dat1
$hiv.dat1$V1
[1] 1 1 1 1 1

$hiv.dat1$V2
[1] 41 42 43 40 44


$hiv.dat2
$hiv.dat2$V1
[1] 0 1 1 0 0

$hiv.dat2$V2
[1] 42 43 40 44 43
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4849476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档