[R] writing 'output.csv' file

Chuck Cleland ccleland at optonline.net
Fri Dec 4 11:28:07 CET 2009


On 12/4/2009 5:12 AM, Maithili Shiva wrote:
> Dear R helpers
>  
> Suppose 
>  
> M <- c(1:10)      #  length(M) = 10
> N <- c(25:50)     #  length(N) = 26 
>  
> I wish to have an outut file giving M and N. So I have tried
>  
> write.csv(data.frame(M, N), 'output.csv', row.names = FALSE)
>  
> but I get the following error message 
>  
> Error in data.frame(M, N) : 
>   arguments imply differing number of rows: 10, 26
>  
> How do I modify my write.csv command to get my output in a single (csv) file irrespective of lengths.

  The first argument to write.csv() is "preferably a matrix or data
frame".  If it is something else, write.csv() will try to make it a data
frame.  You may want to create the data frame like this:

data.frame(M = c(M, rep(NA,length(N) - length(M))), N=N)

    M  N
1   1 25
2   2 26
3   3 27
4   4 28
5   5 29
6   6 30
7   7 31
8   8 32
9   9 33
10 10 34
11 NA 35
12 NA 36
13 NA 37
14 NA 38
15 NA 39
16 NA 40
17 NA 41
18 NA 42
19 NA 43
20 NA 44
21 NA 45
22 NA 46
23 NA 47
24 NA 48
25 NA 49
26 NA 50

> Plese Guide
>  
> Thanks in advance
>  
> Maithili
>  
> 
> 
>       The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. 
> 	[[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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list