Quantcast

Adding a column into the file

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Adding a column into the file

pigpigmeow
Dear all,
I have a lot of problems on R-programming.
for example
my csv. file is ..
Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227
23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493
24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663
25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321
26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856
i calculate the ratio of wrfRH/wrfsolar for each day.

that is  
Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta ratio
21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664  XXXX
22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227  YYYY
23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493 ZZZZ
24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663 AAAA
25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321 BBBB
26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856  CCCC

how to add new column in the original file ? Anyone can help me?!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adding a column into the file

Sarah Goslee
Hi,

On Fri, Jun 1, 2012 at 12:27 PM, pigpigmeow <[hidden email]> wrote:

> Dear all,
> I have a lot of problems on R-programming.
> for example
> my csv. file is ..
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856

Did you read it into R with read.table() or read.csv()?

> i calculate the ratio of wrfRH/wrfsolar for each day.

And got XXXX, YYYY, ZZZZ - I don't think so. Is that part of the question?

myratio <- with(mydata, wrfRH/wrfsolar) might do it.

>
> that is
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta ratio
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664  XXXX
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227  YYYY
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493 ZZZZ
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663 AAAA
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321 BBBB
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856  CCCC
>
> how to add new column in the original file ? Anyone can help me?!

First add it to your data frame with cbind(). Then you can use
write.table() or write.csv() to overwrite your original file, if you
really want to do that.

And please, as I already recommended, read some of the excellent
introductory material available online. This is very basic stuff, and
some evidence that you'd tried to work it out on your would would
encourage the volunteers here to help you out.

Reading the posting guide wouldn't be a bad idea either.

Sarah


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adding a column into the file

David Winsemius
In reply to this post by pigpigmeow

On Jun 1, 2012, at 12:27 PM, pigpigmeow wrote:

> Dear all,
> I have a lot of problems on R-programming.
> for example
> my csv. file is ..

That certainly does not look like any csv file I have ever seen.

> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856
> i calculate the ratio of wrfRH/wrfsolar for each day.
>

If you are confused in your terminology, and you really have a  
data.frame created by read.table, then adding a column to an existing  
data.frame , say the ratio of wrftd divided by wrfta,  is really simple:

dfrm$ratio <- with dfrm, wrftd/wrfta)


> that is
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd  
> wrfta ratio
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664  XXXX
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227  YYYY
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493 ZZZZ
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663 AAAA
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321 BBBB
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856  CCCC
>
> how to add new column in the original file ? Anyone can help me?!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Adding-a-column-into-the-file-tp4632093.html
> Sent from the R help mailing list archive at Nabble.com.
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adding a column into the file

pigpigmeow
This post has NOT been accepted by the mailing list yet.
In reply to this post by Sarah Goslee
Thank!
actually I feel quite confused about R-programming.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adding a column into the file

J Dougherty
In reply to this post by pigpigmeow
On Fri, 1 Jun 2012 09:27:58 -0700 (PDT)
pigpigmeow <[hidden email]> wrote:

> Dear all,
> I have a lot of problems on R-programming.
> for example
> my csv. file is ..
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856
> i calculate the ratio of wrfRH/wrfsolar for each day.
>
> that is  
> Date          wrfRH
 wrfwindspeed wrfrain wrftd wrfta ratio
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664  XXXX
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227  YYYY
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493 ZZZZ
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663 AAAA
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321 BBBB
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856  CCCC
>
> how to add new column in the original file ? Anyone can help me?!
>
First, that is not a csv file.  Your delimiter is a space.  Second, it
appears that the first and second records contain two spaces
between the wrtfa column and the ratio column.  Since you don't provide
an example how how you actually read the data table into R, there's no
way to say what else might be wrong.  If you specify an escaped space
as the delimiter, the read should fail at about the third line.  

Now, if you use R to calculate the ratio the story is different.  I
would guess you simply want to join the calulated field to your data
frame:

To move the new data to a data frame, calculate the new variable and
then assemble the new data frame:

ratio <- dat$wrfRH/dat$wrfsolar

then

newdataframe <- data.frame(dat, ratio)

which should yield what I believe you want.

That of course assumes that what you really want to do is simply add
the new column of calculated values to the original data.  There also
any number of very helpful books about R that can probably help.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adding a column into the file

David Winsemius
In reply to this post by David Winsemius

On Jun 1, 2012, at 4:08 PM, David Winsemius wrote:

>
> On Jun 1, 2012, at 12:27 PM, pigpigmeow wrote:
>
>> Dear all,
>> I have a lot of problems on R-programming.
>> for example my csv. file is ..
>
> That certainly does not look like any csv file I have ever seen.
>
>> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
>> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
>>

>> i calculate the ratio of wrfRH/wrfsolar for each day.
>>
>
> If you are confused in your terminology, and you really have a  
> data.frame created by read.table, then adding a column to an  
> existing data.frame , say the ratio of wrftd divided by wrfta,  is  
> really simple:
>
> dfrm$ratio <- with dfrm, wrftd/wrfta)

And that of course should have been :

dfrm$ratio <- with( dfrm, wrftd/wrfta)

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Loading...