[R] newbie help: simple operations in R

Petr PIKAL petr.pikal at precheza.cz
Tue Jun 2 14:22:22 CEST 2009


Hi

Carletto Rossi <nuovodna at gmail.com> napsal dne 02.06.2009 14:12:12:

> I'm sorry but i verify that Simon solution doesn't work. It makes only 
the 

It partially works

> product between the corrispondent element of the columns (first with 
first, 
> second with seconds, etc,...)
> These are the list of R commands:
> 
> > quattro <- read.csv('new_data.csv', header=TRUE)
> > names(quattro)<-c("x","y")
> > x<-c(NA,quattro$x)
> > y<-c(quattro$y,NA)
> > quattro$t<-quattro$x*quattro$y

x and y is not quattro$x and quattro$y try

quattro$t<-x*y

instead although itself it does not work too. It throws error as x*y has 
one value more then is length of your quattro data frame. You need to drop 
either first or last value from x*y, it depends on you.

e.g.
quattro$t<-(x*y)[-1]

Regards
Petr



> > write.table(quattro,file="with_t.csv",sep="")
> 
> 
> This is the new_data.csv file
> 
> "Y/h","u/U_b"
> 0.16067E-02,-0.11652E-01
> 0.48616E-02,-0.28583E-01
> 0.82005E-02,-0.40079E-01
> 0.11625E-01,-0.47481E-01
> 0.15139E-01,-0.52047E-01
> 0.18743E-01,-0.54658E-01
> 0.22439E-01,-0.55902E-01
> 0.26231E-01,-0.56181E-01
> 0.30121E-01,-0.55777E-01
> 0.34111E-01,-0.54897E-01
> 0.38204E-01,-0.53696E-01
> 0.42403E-01,-0.52267E-01
> 0.46709E-01,-0.50663E-01
> 0.51126E-01,-0.48919E-01
> 0.55658E-01,-0.47059E-01
> 0.60306E-01,-0.45100E-01
> 0.65073E-01,-0.43057E-01
> 0.69964E-01,-0.40940E-01
> 0.74980E-01,-0.38733E-01
> 0.80126E-01,-0.36428E-01
> 0.85404E-01,-0.34044E-01
> 0.90817E-01,-0.31620E-01
> 0.96371E-01,-0.29153E-01
> 0.10207E+00,-0.26602E-01
> 0.10791E+00,-0.23957E-01
> 0.11390E+00,-0.21238E-01
> 
> This is the with_t.csv new file:
> 
> "x","y","t"
> "1",0.0016067,-0.011652,-1.87212684e-05
> "2",0.0048616,-0.028583,-0.0001389591128
> "3",0.0082005,-0.040079,-0.0003286678395
> "4",0.011625,-0.047481,-0.000551966625
> "5",0.015139,-0.052047,-0.000787939533
> "6",0.018743,-0.054658,-0.001024454894
> "7",0.022439,-0.055902,-0.001254384978
> "8",0.026231,-0.056181,-0.001473683811
> "9",0.030121,-0.055777,-0.001680059017
> "10",0.034111,-0.054897,-0.001872591567
> "11",0.038204,-0.053696,-0.002051401984
> "12",0.042403,-0.052267,-0.002216277601
> "13",0.046709,-0.050663,-0.002366418067
> "14",0.051126,-0.048919,-0.002501032794
> "15",0.055658,-0.047059,-0.002619209822
> "16",0.060306,-0.0451,-0.0027198006
> "17",0.065073,-0.043057,-0.002801848161
> "18",0.069964,-0.04094,-0.00286432616
> "19",0.07498,-0.038733,-0.00290420034
> "20",0.080126,-0.036428,-0.002918829928
> "21",0.085404,-0.034044,-0.002907493776
> "22",0.090817,-0.03162,-0.00287163354
> "23",0.096371,-0.029153,-0.002809503763
> "24",0.10207,-0.026602,-0.00271526614
> "25",0.10791,-0.023957,-0.00258519987
> "26",0.1139,-0.021238,-0.0024190082
> 
> Thanks.
> Regards
> 
> PS Can i avoid to print the column with "1","2","3","4", etc... ??
> 
> 
> 

> 2009/6/2 Petr PIKAL <petr.pikal at precheza.cz>
> Hi
> 
> r-help-bounces at r-project.org napsal dne 02.06.2009 13:20:26:
> 
> > The solution I provided should work
> >
> > First off all give your data frame shorter and easier names e.g.
> >
> > names(quatrro)<-c("x","y",etc)
> >
> > since you want y(n)*x(n+1) an easy way to code this is to add on an 
"NA"
> to
> > the start of x so everything shifts down a row and add an NA on the 
end
> of Y
> > so that X and Y are the same length. look up the concatentae function 
?c
> to
> > understand this.
> >
> > Data frame is not a problem. In that case its...
> >
> > x<-c(NA,quattro$x)
> > y<-c(quattro$y,NA)
> > quattro$t<-quattro$x*quatrro$y

> Above will not work as your result has one value more then original data
> frame
> 
> > df$t<-c(df$x,NA)*c(NA,df$y)
> Error in `$<-.data.frame`(`*tmp*`, "t", value = c(NA, 3, 28, NA)) :
>  replacement has 4 rows, data has 3
> >
> 
> She need to drop the appropriate one.
> 
> Regards
> Petr
> 
> >
> > then
> >
> > write.table(quattro,file="where you want the table to go.csv",sep="")
> >
> > HTH, Si.
> >
> >
> >
> >   ----- Original Message -----
> >   From: Carletto Rossi
> >   To: Simon Pickett ; r-help at r-project.org
> >   Sent: Tuesday, June 02, 2009 12:03 PM
> >   Subject: Re: [R] newbie help: simple operations in R
> >
> >
> >   I' ve tried your suggestions but the results are wrong.I don't
> > understand...i explain my request in a new way.
> >   I have this table named data-4-bk.csv
> >
> >   "Y/h","u/U_b","v/U_b","uu/U_b^2","vv/U_b^2","uv/U_b^2","k/U_b^2"
> >
> 
0.16067E-02,-0.11652E-01,0.30712E-04,0.11377E-02,0.37886E-06,-0.12657E-05,0.13570E-02
> >
> 
0.48616E-02,-0.28583E-01,-0.10085E-03,0.72322E-02,0.44299E-04,-0.17874E-04,0.78991E-02
> >
> 
0.82005E-02,-0.40079E-01,-0.25124E-03,0.14272E-01,0.23333E-03,-0.75078E-04,0.15054E-01
> >
> 
0.11625E-01,-0.47481E-01,-0.50081E-03,0.19783E-01,0.60134E-03,-0.17623E-03,0.20764E-01
> >
> 
0.15139E-01,-0.52047E-01,-0.78504E-03,0.23607E-01,0.11297E-02,-0.31123E-03,0.25042E-01
> >
> 
0.18743E-01,-0.54658E-01,-0.11273E-02,0.26189E-01,0.17884E-02,-0.46941E-03,0.28287E-01
> >
> 
0.22439E-01,-0.55902E-01,-0.15016E-02,0.27935E-01,0.25450E-02,-0.64447E-03,0.30825E-01
> >
> 
0.26231E-01,-0.56181E-01,-0.19149E-02,0.29136E-01,0.33728E-02,-0.83204E-03,0.32881E-01
> >
> 
0.30121E-01,-0.55777E-01,-0.23570E-02,0.29974E-01,0.42480E-02,-0.10299E-02,0.34591E-01
> >
> 
0.34111E-01,-0.54897E-01,-0.28303E-02,0.30577E-01,0.51528E-02,-0.12376E-02,0.36045E-01
> >
> 
0.38204E-01,-0.53696E-01,-0.33293E-02,0.31034E-01,0.60717E-02,-0.14533E-02,0.37305E-01
> >
> 
0.42403E-01,-0.52267E-01,-0.38533E-02,0.31403E-01,0.69931E-02,-0.16763E-02,0.38415E-01
> >
> 
0.46709E-01,-0.50663E-01,-0.43996E-02,0.31712E-01,0.79083E-02,-0.19064E-02,0.39403E-01
> >
> >   I give these R commands to import the file
> >
> >   quattro <- read.csv('data-4-bk.csv', header=TRUE)
> >   attach(quattro)
> >   names(quattro)
> >
> >   Then i'd like to calculate the new variable "t" defined as:
> >
> >   First element of u/U_b column * Second element of Y/h column
> >   Second element of u/U_b column * Third element of Y/h column
> >   etc.. etc..
> >
> >   Now i' d like to print the values of t as a new column on the 
original
> data-4-bk.csv
> >
> >   Thanks and sorry for my newbie request.
> >
> >
> >
> >
> >
> >
> >   2009/6/2 Simon Pickett <simon.pickett at bto.org>
> >
> >     you could use a loop but maybe easier would be
> >     x<-c(NA,x)
> >     y<-c(y,NA)
> >     t<-x*y
> >
> >     use write.table() to write the table to your hard drive
> >
> >     e.g.
> >
> >     write.table(t, file="C:/Documents and Settings/simonp/My
> Documents/RELU/
> > GIS data/Land Cover Map working/squares plus adjoining 
parcels/Scotland/
> > scotland adj squares.csv", sep = ",",row.names = F)
> >
> >     HTH, Si.
> >
> >     ----- Original Message ----- From: "Carletto Rossi"
> <nuovodna at gmail.com>
> >     To: <r-help at r-project.org>
> >     Sent: Tuesday, June 02, 2009 11:22 AM
> >     Subject: [R] newbie help: simple operations in R
> >
> >
> >
> >       Hi, i' d like to use R for simple calculations. I show you an
> examples to
> >       make clear my help request
> >
> >       I' ve a file .csv like this (my real file is composed by 10.000
> lines and 8
> >       columns)
> >
> >       x y
> >       3 4
> >       1 7
> >
> >       I' ve imported in R correctly. Now i want create a new variable
> named "t"
> >       and t is defined throught this relation:
> >
> >       t = 4 (second element on x column) * 4 (first element on y 
column)
> >
> >       in what way can i calculate t and print its value on an external
> file or,
> >       better, append t values as a column on my original csv???
> >
> >       Thanks
> >
> >       Emanuele
> >
> >
> >       [[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.
> >
> >
> >
> >
> >
> >
> >    [[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.




More information about the R-help mailing list