Skip to content
Prev 327610 / 398502 Next

R function

Rui has shown you a much more efficient way to code your
function in R. To fix the code you posted, you need to add
brackets around the loop, test x[i] instead of i (which is
always >= 1), and get the length of the loop from x not
pah1$P. Without the brackets only the first if() is included
in the for loop:

T <- function(x) {
for (i in 1:length(x)) {
	if (x[i] >= 1) 
		print("Combustion")
	if (x[i] < 1) 
		print("Petroleum")
	}
}

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Rui
Barradas
Sent: Monday, July 29, 2013 3:46 AM
To: javad bayat
Cc: r-help at r-project.org
Subject: Re: [R] R function

Hello,

Sorry, that should be


T <- function(x){
	ifelse(x > = 1, "Combustion", "Petroleum")
}



Rui Barradas

Em 29-07-2013 09:32, Rui Barradas escreveu:
it cant be
equal to one
"Petroleum".
reproducible code.
code.

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