[R] if else and loop for code in R

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Sep 25 15:01:23 CEST 2009


It looks like you are trying to mimic the SAS data step. In R you can
vectorise this.

a_data <- read.table("D:/SNP/copy.sas", header=T, sep="\t") 
a_data$stat <- with(a_data, ifelse(truck < 0, 0, ifelse(cars > 100, 0,
cars)))
a_data$i <- seq_len(nrow(a_data))
outTable <- a_data[, c("i", "stat", "truck")]

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Minh Duy Mai
Verzonden: vrijdag 25 september 2009 13:53
Aan: r-help at R-project.org
CC: minhmaiduy at yahoo.com
Onderwerp: [R] if else and loop for code in R

I am using if else and loop to sortout the data set that is the values
less than o or more than 100 will be chosen.I could not get outTable
with loop.
Please help me to correct the code:
I USED:

# Read
a_data <- read.table("D:/SNP/copy.sas", header=T, sep="\t") tr <-
a_data$truck ca <- a_data$cars length <- nrow(a_data) outTable <-
matrix(nrow=length,ncol=3)

stat <- for (i in 1:length) {
if (tr<0) {0} else
if (ca>100) {0}else
{ca}
outTable <- c(i, stat, tr)
}
# Writing the output file
   colnames(outTable) <- c("number", "stat", "tr")
    
write.table(outTable,"D:/SNP/mixed.txt",append=FALSE,quote=FALSE,sep='\t
',
row.names=F)
# Graph
plot(stat, type="o", col="red", axes=FALSE, ann=FALSE) # Create a title
with a red, bold/italic font title(main="Autos", col.main="red",
font.main=4) # Start PNG device driver to save output to figure.png
png(filename="D:/SNP/figure.png", height=295, width=300, bg="white")
.....................
COMPLAIN
Error: object 'stat' not found
In addition: Warning message:
In if (tr < 0) { :
   the condition has length > 1 and only the first element will be used
...................

Thank alot

______________________________________________
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.

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




More information about the R-help mailing list