简体   繁体   English

R中的data.frame

[英]data.frame in R

I am a beginner in R, and I want to create an xlsx file from R. My data frame is defined as: 我是R的初学者,我想从R创建一个xlsx文件。我的数据框定义为:

SP : List of 8
 $ WL          : num [1:1201] 200 200 201 202 202 ...
 $ t           : num [1:872, 1] 0.499 0.999 1.499 1.999 2.499 ...
 $ SP          : int [1:872, 1:1201, 1] 380 405 447 431 419 436 439 434 422 447 ...
 $ TF          : int [1:872, 1] 0 1 2 3 4 5 6 7 8 9 ...
 $ SpectraCount: num 872
 $ TStamp      : chr "12/01/2011 03:09:32PM"
 $ IT          : int 100
 $ SourceCount : num 1 

I want to transform the data.frame into a table with one column for each value of WL , and one row for each value of t , with the values inside the table as the SP for that combination of WL and t . 我想将data.frame转换成一个表,其中WL每个值一列, t每个值一行,表中的值作为WLt组合的SP。 The resulting table would look like: 结果表如下所示:

               WL 1,.......... 1201
            t
            1         (SP values)
            .
            827

If i put 如果我放

write.csv(SP1, file="SP1.csv", row.names = FALSE)

then i have Error in data.frame(WL = c(200, 200.5, 201, 201.5, 202, 202.5, 203, 203.5, : arguments imply differing number of rows: 1201, 872, 1 然后我在data.frame中出错(WL = c(200,200.5,201,201.5,202,202.5,203,203.5,:参数暗示不同的行数:1201,872,1

Maybe generating a csv file would be enough. 也许生成一个csv文件就足够了。 Excel can read csv files. Excel可以读取csv文件。 See write.csv for this. 参见write.csv

Otherwise, there is write.xls from the dataframes2xls package, or write.xlsx from the xlsx package. 否则,有write.xlsdataframes2xls包,或write.xlsxxlsx包。

No Excel interface I know of can deal with ragged lists - you will need to convert that to a rectangular data structure. 据我所知,没有Excel界面可以处理参差不齐的列表-您需要将其转换为矩形数据结构。

You could of course loop over the columns of your list and write those, but I think you'd be better off just padding your list into a data.frame: 您当然可以循环使用列表的列并编写它们,但是我认为最好将列表填充到data.frame中:

> l <- list(a="foo", b=c(1,2,3), d=(1.0,2.0,3.0,4.0))
> rows <- max(unlist(lapply(l, length)))
> padded <- lapply(l, function(col) c(col, rep(NA, rows-length(col))))
> as.data.frame(padded)
     a  b d
1  foo  1 1
2 <NA>  2 2
3 <NA>  3 3
4 <NA> NA 4

Then, you can use any of the Excel packages, with my favourite XLConnect : 然后,您可以将任何Excel软件包与我最喜欢的XLConnect一起使用

> library(XLConnect)
> writeWorksheetToFile(file="out.xlsx", data=as.data.frame(padded), sheet="Output")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM