Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.frame without ruining column names

Tags:

r

Is there a way to use data.frame without it ruining the column names?

I have following structure:

$`Canon PowerShot`
[1] 9.997803e-01 9.997318e-01 3.327920e-01 3.327920e-01 9.988220e-01
[6] 4.030871e-05 4.928497e-05

$`Casio Exilim`
[1] 5.322024e-06 9.999646e-01 5.322024e-06 5.322024e-06 9.999646e-01
[6] 5.322024e-06 9.999646e-01

$FinePix
[1] 3.850036e-05 9.998887e-01 6.650074e-02 6.650074e-02 9.998465e-01
[6] 9.998465e-01 4.345598e-05

$`Kodak EasyShare`
[1] 3.548812e-05 9.998604e-01 3.996137e-01 3.996137e-01 9.987841e-01
[6] 3.179604e-05 2.789861e-05

$`Nikon Coolpix series`
[1] 9.156401e-02 9.998091e-01 1.995972e-01 1.995972e-01 9.996341e-01
[6] 7.033741e-05 8.499410e-05

but after using do.call(data.frame, my_list), I get this:

  Canon.PowerShot Casio.Exilim      FinePix Kodak.EasyShare
1    9.997803e-01 5.322024e-06 3.850036e-05    3.548812e-05
2    9.997318e-01 9.999646e-01 9.998887e-01    9.998604e-01
3    3.327920e-01 5.322024e-06 6.650074e-02    3.996137e-01
4    3.327920e-01 5.322024e-06 6.650074e-02    3.996137e-01
5    9.988220e-01 9.999646e-01 9.998465e-01    9.987841e-01
6    4.030871e-05 5.322024e-06 9.998465e-01    3.179604e-05
7    4.928497e-05 9.999646e-01 4.345598e-05    2.789861e-05
  Nikon.Coolpix.series
1         9.156401e-02
2         9.998091e-01
3         1.995972e-01
4         1.995972e-01
5         9.996341e-01
6         7.033741e-05
7         8.499410e-05

(note there are . instead of ' ' in column names)

like image 322
Arg Avatar asked Sep 06 '12 06:09

Arg


2 Answers

data.frames in R are actually lists. Therefore, this is also valid:

data.frame(my_list, check.names = FALSE)

Knowing this opens up the possibilities of using lapply on data.frames, which I think is pretty cool:

my_data <- data.frame(my_list, check.names = FALSE)
lapply(my_data, IQR)
like image 181
Zach Avatar answered Nov 04 '22 12:11

Zach


You can stop R changing the names to syntatically valid names by setting check.names = FALSE. See ?data.frame for details.

# assuming your data is in a list called my_list
do.call(data.frame, c(my_list, check.names = FALSE))
like image 20
mnel Avatar answered Nov 04 '22 10:11

mnel