r - Write.table as text file with differing number of rows -


i want share output of analysis in r txt file. can 1 here know of:

write.table(s$maininformation,  file = "output.txt",   row.names = false, sep = "\t",    quote = false, fileencoding = "utf-8", append = true)  write.table(s$annualproduction,  file = "output.txt",   row.names = false, sep = "\t",    quote = false, fileencoding = "utf-8", append = true)  write.table(s$annualgrowthrate,  file = "output.txt",   row.names = false, sep = "\t",    quote = false, fileencoding = "utf-8", append = true)  write.table(s$mostprodauthors,  file = "output.txt",   row.names = false, sep = "\t",    quote = false, fileencoding = "utf-8", append = true) 

the documents combined 1 txt file. however, if want in 1 step, i.e. s rather s$, error code, not surprisingly, argumetns imply differing number of rows.

write.table(s,  file = "output.txt",    row.names = false, sep = "\t",     quote = false, fileencoding = "utf-8") 

error in (function (..., row.names = null, check.rows = false, check.names = true, : arguments imply differing number of rows: 16, 25, 1, 10

of course, don't care that... don't know yet how work lapply helpful_ thanks! scw

 str(s) list of 9  $ maininformation  : chr [1:16] "\n\nmain information data\n\n" "articles                              1374 \n" "sources (journals, books, etc.)       736 \n" "keywords plus (id)                    1172 \n" ...  $ annualproduction :'data.frame':  25 obs. of  2 variables:   ..$ year    : factor w/ 25 levels "1985","1988",..: 1 2 3 4 5 6 7 8 9 10 ...   ..$ articles: int [1:25] 1 1 1 1 3 1 2 2 3 2 ...  $ annualgrowthrate : num 20.9  $ mostprodauthors  :'data.frame':  10 obs. of  4 variables:   ..$ authors                :class 'asis'  chr [1:10] "nye,j.      " "grix,j      " "ogunnubi,o  " "pamment,j   " ...   ..$ articles               :class 'asis'  chr [1:10] "16" " 8" " 8" " 8" ...   ..$ authors                :class 'asis'  chr [1:10] "nye,j.    " "pamment,j " "chong,a   " "lee,j     " ...   ..$ articles fractionalized:class 'asis'  chr [1:10] "16.00" " 8.00" " 6.00" " 5.00" ...  $ mostcitedpapers  :'data.frame':  10 obs. of  3 variables:   ..$ paper         :class 'asis'  chr [1:10] "santos f;eisenhardt k,(2009),acad. manage. j.                                            " "hyde-price a,(2006),j. eur. public policy                                                " "nye j.,(2008),ann. am. acad. polit. soc. sci.                                            " "nye j.,(2003),the paradox of am. power: why world's superpower can't go alone" ...   ..$ tc            :class 'asis'  chr [1:10] "262" "133" "120" "119" ...   ..$ tcperyear     :class 'asis'  chr [1:10] "32.75" "12.09" "13.33" " 8.50" ...  $ mostprodcountries:'data.frame':  10 obs. of  3 variables:   ..$ country  :class 'asis'  chr [1:10] "usa           " "england       " "australia     " "china         " ...   ..$ articles :class 'asis'  chr [1:10] "255" " 96" " 81" " 67" ...   ..$ freq     :class 'asis'  chr [1:10] "0.2621" "0.0987" "0.0832" "0.0689" ...  $ tcpercountries   :'data.frame':  10 obs. of  3 variables:   ..$ country                  :class 'asis'  chr [1:10] "usa           " "england       " "australia     " "united kingdom" ...   ..$ total citations          :class 'asis'  chr [1:10] "1701" " 708" " 303" " 297" ...   ..$ average article citations: chr [1:10(1d)] "6.671" "7.375" "3.741" "6.319" ...   .. ..- attr(*, "dimnames")=list of 1   .. .. ..$ co: chr [1:10] "usa" "england" "australia" "united kingdom" ...  $ mostrelsources   :'data.frame':  10 obs. of  2 variables:   ..$ sources       :class 'asis'  chr [1:10] "international journal of history of sport                                         " "international journal of communication                                                " "international journal of cultural policy                                              " "journal of contemporary china                                                         " ...   ..$ articles      :class 'asis'  chr [1:10] "19" "16" "16" "14" ... $ mostrelkeywords  :'data.frame':   10 obs. of  4 variables: ..$ author keywords (de)     :class 'asis'  chr [1:10] "soft power        " "china             " "public diplomacy  " "foreign policy    " ... ..$ articles                 :class 'asis'  chr [1:10] "387" "119" " 74" " 50" ... ..$ keywords-plus (id)       :class 'asis'  chr [1:10] "soft power      " "foreign policy  " "politics        " "china           " ... ..$ articles                 :class 'asis'  chr [1:10] "112" " 47" " 41" " 39" ... 

is file meant human-readable or machine-readable?

if human-readable, capture.output() suggested above should sufficient.

lst <- list(a=1:8,              b=1.2e+11,            df1=data.frame(foo=1:5/4, bar=1:5/3),            df2=as.data.frame(matrix(runif(16), 4))) capture.output(lst, file="list.txt") 

otherwise, if insist on putting things side-by side, might option

max_l <- max(rapply(lst, length)) as.data.frame(rapply(lst, function(x) 'length<-'(x, max_l), how="list"))  #          b df1.foo   df1.bar    df2.v1    df2.v2    df2.v3     df2.v4 # 1  1 1.2e+11    0.25 0.3333333 0.3978436 0.1971467 0.5619881 0.01103607 # 2  2      na    0.50 0.6666667 0.9556758 0.1153423 0.7327180 0.90631526 # 3  3      na    0.75 1.0000000 0.6533494 0.9959655 0.8708056 0.77065363 # 4  4      na    1.00 1.3333333 0.3287437 0.3792767 0.5721703 0.38250462 # 5  5      na    1.25 1.6666667        na        na        na         na # 6  6      na      na        na        na        na        na         na # 7  7      na      na        na        na        na        na         na # 8  8      na      na        na        na        na        na         na 

and write file other data.frame.

if file meant machine-readable, maybe results further processed using other software, better approach use more specialized file format plain text. json or xml.

writing json simple as

library(rjsonio) write(tojson(lst), file="list.json") 

the resulting file still human-readable, think.

{ "a": [ 1, 2, 3, 4, 5, 6, 7, 8 ], "b": 1.2e+11, "df1": {  "foo": [   0.25,    0.5,   0.75,      1,   1.25 ],  "bar": [ 0.33333, 0.66667,      1, 1.3333, 1.6667 ]  }, "df2": {  "v1": [ 0.094046, 0.049654, 0.82116, 0.82932 ],  "v2": [ 0.65473, 0.13283, 0.34181, 0.73137 ],  "v3": [ 0.90729, 0.6962, 0.24158, 0.64411 ],  "v4": [ 0.28075, 0.95764, 0.1584, 0.41834 ]  }  } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -