[R] writing 'output.csv' file

0 views
Skip to first unread message

Maithili Shiva

unread,
Dec 4, 2009, 5:12:18 AM12/4/09
to r-h...@r-project.org
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.
 
Plese Guide
 
Thanks in advance
 
Maithili
 


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
[[alternative HTML version deleted]]

Chuck Cleland

unread,
Dec 4, 2009, 5:28:07 AM12/4/09
to Maithili Shiva, r-h...@r-project.org
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-h...@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

______________________________________________
R-h...@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.

Maithili Shiva

unread,
Dec 4, 2009, 5:49:32 AM12/4/09
to Johannes Signer, r-h...@r-project.org, ccle...@optonline.net

Dear Mr Signer and Mr Cleland,
 
Thanks a lot for you great help. However, the output which I am getting is as given below -
 


x

1;25

2;26

3;27

4;28

5;29

6;30

7;31

8;32

9;33

10;34

 ;35

 ;36

 ;37

 ;38

 ;39

 ;40

 ;41

 ;42

 ;43

 ;44

 ;45

 ;46

 ;47

 ;48

 ;49

 ;50
 
However, my requirement is I should get the csv file as
 
M             N
1             25
2             26
3             27
................
 
10           34
               35
               36
               37

......................
......................
               50
 
So that I can acrry out further calcualtions on this output file. Please guide.
 
Regards
 
Maithili

--- On Fri, 4/12/09, Johannes Signer <j.m.s...@gmail.com> wrote:


From: Johannes Signer <j.m.s...@gmail.com>
Subject: Re: [R] writing 'output.csv' file
To: "Maithili Shiva" <maithil...@yahoo.com>
Date: Friday, 4 December, 2009, 10:29 AM


Hello,

maybe that helps:

write.csv(paste((c(m,rep(" ",length(N)-length(M)))),n, sep=";"), "output.csv", row.names=F)

Johannes


On Fri, Dec 4, 2009 at 11:12 AM, Maithili Shiva <maithil...@yahoo.com> 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.
 

Plese Guide
 
Thanks in advance
 
Maithili
 


     The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
       [[alternative HTML version deleted]]

______________________________________________
R-h...@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.

[[elided Yahoo spam]]

[[alternative HTML version deleted]]

jim holtman

unread,
Dec 4, 2009, 7:37:35 AM12/4/09
to Maithili Shiva, r-h...@r-project.org, ccle...@optonline.net
The csv file is exactly as you describe it. ";35" represents two columns of
data. If you read it is, you will probably get NA as the value in the first
column. So what problem are you having in reading in the data?

> x <- read.csv(textConnection("8;32
+ 9;33
+ 10;34
+ ;35
+ ;36
+ ;37
+ ;38"), header=FALSE, sep=';')
> closeAllConnections()
> x
V1 V2
1 8 32
2 9 33
3 10 34
4 NA 35
5 NA 36
6 NA 37
7 NA 38

> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>


> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>
> [[elided Yahoo spam]]
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-h...@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<http://www.r-project.org/posting-guide.html>


> and provide commented, minimal, self-contained, reproducible code.
>
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

Reply all
Reply to author
Forward
0 new messages