[R] create dataframe using structure ()

William Dunlap wdunlap at tibco.com
Sat May 10 20:36:54 CEST 2014


You made the same error as the first time - a misplaced parenthesis.
Make the last 2 lines
   .Names = c("row", "stimulation", "positivity", "group", "copy")),
  row.names=c(NA, 120L), class="data.frame")
instead of
    .Names = c("row", "stimulation", "positivity", "group", "copy"),
  row.names = c(NA, -120L)),  class = "data.frame")

The proper code makes row.names an attribute of the list containing
the columns, the bad code makes it another column and makes a
data.frame with no row names.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, May 10, 2014 at 11:04 AM, Luigi Marongiu
<marongiu.luigi at gmail.com> wrote:
> Hello,
> I have tried to apply this approach -- which worked for this example
> -- to a larger dataset. Although I have not received error messages,
> the variable I have set (my.data) is empty.
> Do you have any tip about this?
> Many thanks
> Luigi
>
> # code::
> my.data<-structure(list(
>   column_1 = 1:120,
>   column_2 = structure(c(
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8,
>     1,2,3,4,5,6,7,8),
>     .Label = c("a", "b", "c", "d", "e", "f", "g", "h"), class = "factor"),
>   column_3 = structure(c(
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,
>     0,0,0,0,0,0,0,0)),
>   column_4 = structure(c(
>     0,  0,  0,  0,    0,    0,    0,    0,
>     1,    1,    1,    1,    1,    1,    1,    1,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     1,  1,    1,    1,    1,    1,    1,    1,
>     0,  0,    0,    0,    0,    0,    0,    0,
>     0,  0,    0,    0,    0,    0,    0,    0)),
>   column_5 = structure(c(
>     192.0519108,183.6403531,53.46798757,83.60638077,69.60749873,159.4706861,256.8765622,499.2899303,
>     2170.799076,1411.349719,2759.472348,2098.973397,2164.739515,1288.676574,1611.486543,6205.229575,
>     870.7424981,465.9967135,191.8962375,864.0937485,2962.693675,1289.259137,2418.651212,7345.712517,
>     0,168.1198893,674.4342961,101.1575401,47.81596237,0,0,1420.793922,
>     142.6871331,5.466468742,291.9564635,80.73914133,73.02239621,64.47806871,144.3543635,3167.959757,
>     3164.748333,1092.634557,28733.20269,1207.87783,729.6090973,151.8706088,241.2466141,9600.963594,
>     1411.718287,12569.96285,1143.254476,6317.378481,16542.27718,79.68025792,1958.495138,7224.503437,
>     208.4382941,69.48609769,656.691151,0.499017582,7114.910926,187.6296174,41.73980805,8930.784541,
>     4.276752185,0.432300363,60.89228665,1.103924786,0.490686366,1.812993239,7.264531581,1518.610307,
>     2172.051528,595.8513744,17141.84336,589.6565971,1340.287628,117.350942,593.7034054,24043.61463,
>     0,81.83292179,1539.864321,36.41722958,8.385131047,161.7647376,65.21615696,7265.573875,
>     97.84753179,154.051827,0.613835842,10.06138851,45.04879285,176.8284258,18795.75462,30676.769,
>     5780.34957,944.2200834,2398.235596,1083.393165,2541.714557,1251.670895,1547.178549,1792.679176,
>     3067.988416,8117.210173,23676.02226,8251.937547,17360.80494,18563.61561,16941.865,31453.96708,
>     2767.493803,4796.33016,12292.93705,3864.657567,9380.673835,14886.44683,8457.88646,26050.47191)),
>   .Names = c("row", "stimulation", "positivity", "group", "copy"),
> row.names = c(NA, -120L)),  class = "data.frame")
>
> attach(my.data)
>
> # results:
>> my.data
> [1] column_1  column_2  column_3  column_4  column_5  .Names    row.names
> <0 rows> (or 0-length row.names)
>
> On Thu, Mar 13, 2014 at 7:29 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>> Hello,
>>
>> You're misplacing one close parenthesis. See the commented lines below.
>>
>>
>>
>> structure(list(
>> column_1 = 1:32,
>> column_2 = structure(c(1, 2, 3, 4, 5, 6, 7, 8,
>> 1, 2, 3, 4, 5, 6, 7, 8,
>> 1, 2, 3, 4, 5, 6, 7, 8,
>> 1, 2, 3, 4, 5, 6, 7, 8),
>> .Label =
>> c("Unstimulated", "ESAT6", "CFP10", "Rv3615c", "Rv2654", "Rv3879",
>> "Rv3873", "PHA"),
>> class = "factor"),
>> column_3 =
>> structure(c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
>> 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4), .Label = c("bd", "2m", "1m",
>> "0.5m"), class = "factor"),
>> column_4 =
>> c(71.62097178, 2.892223468, 144.8618621, 40.90079262, 37.0516856,
>> 32.78206822, 72.44424152, 1512.516293, 3164.748333, 1092.634557,
>> 28733.20269, 1207.87783, 729.6090973, 151.8706088, 241.2466141,
>> 9600.963594, 682.865522, 5869.375226, 554.8924896, 2982.759858,
>> 7690.028092, 40.37298719, 942.3549279, 3403.679967, 103.9862389,
>> 35.28562613, 321.5985377, 0.274424607, 3352.874906, 93.76421187,
>> 21.3709382, 4193.183281)),  # close 2 )) here
>> .Names = c("row", "stimulation", "type", "copy"), row.names = c(NA,
>> -32L), class = "data.frame")  # and only one ) after df
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 13-03-2014 18:58, Luigi Marongiu escreveu:
>>>
>>> dear all,
>>> I have tried to create a dataframe using the structure() function, but it
>>> did not really work. In column 1 i have the row number, in column 2 and 3
>>> factors (8 and 4 levels respectively) and in column 4 the actual data.
>>> any clue on what went wrong?
>>> best regards
>>> luigi
>>>
>>> my.data<-structure(list(
>>> column_1 = 1:32,
>>> column_2 = structure(c(1, 2, 3, 4, 5, 6, 7, 8,
>>> 1, 2, 3, 4, 5, 6, 7, 8,
>>> 1, 2, 3, 4, 5, 6, 7, 8,
>>> 1, 2, 3, 4, 5, 6, 7, 8),
>>> .Label =
>>> c("Unstimulated", "ESAT6", "CFP10", "Rv3615c", "Rv2654", "Rv3879",
>>> "Rv3873", "PHA"),
>>> class = "factor"),
>>> column_3 =
>>> structure(c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
>>> 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4), .Label = c("bd", "2m", "1m",
>>> "0.5m"), class = "factor"),
>>> column_4 =
>>> c(71.62097178, 2.892223468, 144.8618621, 40.90079262, 37.0516856,
>>> 32.78206822, 72.44424152, 1512.516293, 3164.748333, 1092.634557,
>>> 28733.20269, 1207.87783, 729.6090973, 151.8706088, 241.2466141,
>>> 9600.963594, 682.865522, 5869.375226, 554.8924896, 2982.759858,
>>> 7690.028092, 40.37298719, 942.3549279, 3403.679967, 103.9862389,
>>> 35.28562613, 321.5985377, 0.274424607, 3352.874906, 93.76421187,
>>> 21.3709382, 4193.183281),
>>> .Names = c("row", "stimulation", "type", "copy"), class = "data.frame"))
>>> attach(my.data)
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list