[R] newbie help: simple operations in R

Petr PIKAL petr.pikal at precheza.cz
Tue Jun 2 13:37:12 CEST 2009


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