# SubDataFrame to DataFrame

**URL:** https://discourse.julialang.org/t/subdataframe-to-dataframe/2241
**Category:** Statistics
**Tags:** question
**Created:** [February 22, 2017, 7:51pm UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241 "2017-02-22T19:51:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Diego\_Javier\_Zea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diego_javier_zea/32/1858_2.png) [@Diego\_Javier\_Zea](https://discourse.julialang.org/u/Diego_Javier_Zea)
#### Post date: [February 22, 2017, 7:51pm UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241/1 "2017-02-22T19:51:01Z")

</div>

Hi!  
I was trying to use a function that not accept `SubDataFrames` inside `by()` (in particular [https://github.com/JuliaInterop/RCall.jl/issues/167](https://github.com/JuliaInterop/RCall.jl/issues/167)). I found that there is not way to get a `DataFrame` from a `SubDataFrame`. It would be great to have a convert method to use in this kind of cases (even if it generates copies).  
Best,

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [February 22, 2017, 8:10pm UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241/2 "2017-02-22T20:10:29Z")

</div>

I thought we had a converter at one time. Anyway, it’s easy to write:

```julia
todf(x::SubDataFrame) = x.parent[x.rows,:]

```

It does make copies. To avoid copies, you could convert to a DataFrame where each column is a view into the original.That’s a bit more work, so consider that an exercise for the reader 🙂 .

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [February 22, 2017, 8:45pm UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241/3 "2017-02-22T20:45:30Z")

</div>

Actually, the column view version is pretty easy, too. Note: not tested much.

```julia
todfview(x::SubDataFrame) = DataFrame(Any[view(col, x.rows) for col in x.parent.columns], names(x))

```

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 23, 2017, 9:24am UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241/4 "2017-02-23T09:24:31Z")

</div>

Doesn’t `copy` on a `view` of type `T` always create an object of type `T`?  
Of course in this case it may be cool to not copy. And as @Diego_Javier_Zea notes in his issue the solution should be to define the function on AbstractDataFrame.

---

<div class="post-metadata">

### Author: ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)
#### Post date: [August 29, 2018, 9:47am UTC](https://discourse.julialang.org/t/subdataframe-to-dataframe/2241/5 "2018-08-29T09:47:21Z")

</div>

I tried these on Julia 0.7 but it doesn’t seem to work… Is there a new / easiest way to convert a subdatframe to a dataframe?
