[R] conditional value assignment

2012-10-21 Thread penguins
Hi,

I am trying to assign values to records based conditionally on other records
within a dataframe. For example, in the following dataframe the "NA" value
in dat$age would be replaced if the age status for that individual and
specific year can be found in another record. Ideally this would then also
assign the same age status if the individual is recorded in a later year.

id<-c(1,1,1,1,2,2,2,2,2, 3,3,3, 4,4) 
year<-c(1,1,1,2, 2,2,3,2,2, 2,3,4, 8,8)  
age<-c("Adult","NA","NA","NA", "Adult","NA","NA","NA", "Adult",
"NA","Adult","NA", "NA","Adult")
dat<-cbind(id,year,age)
dat<-data.frame(dat)

I am attempting to transform the age variable to;
"Adult","Adult","Adult","Adult",
"Adult","Adult","Adult","Adult","Adult","NA","Adult","Adult","Adult","Adult"

I have used the following code to cross-reference the age data across
records of the same year but am having trouble getting this work. I am also
unsure how to cross-reference this to assign age status to records of the
same individual recorded in later years.

id<-unique(dat$id)
year<-c(1,2,3,8)
namer<-cbind(pit,season)
namer<-data.frame(namer)

for (i in 1:nrow(namer)) {
dat$age[dat$id == namer[i,1] & dat$year== namer[i,2]] <- "Adult"
}

Any help would be gratefully recieved,

Thanks




--
View this message in context: 
http://r.789695.n4.nabble.com/conditional-value-assignment-tp4646945.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] conditional value assignment

2012-10-22 Thread penguins
Thanks, that has worked like a dream!



--
View this message in context: 
http://r.789695.n4.nabble.com/conditional-value-assignment-tp4646945p4646988.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] Remove records from a large dataframe

2012-10-22 Thread penguins
Hi, I am trying to remove a series of records from a large dataframe. The
script I have written works fine but takes a long time to run. Can anyone
suggest a quicker way to do this?

Here is an example of the code I've written. The end result of this bit of
code would be a dataframe with any records relating to ID 1 or ID 4 removed:

#dataframe
id <- c(1,1,1,1,2,2,2,2,2, 3,3,3, 4,4)
year <- c(1,1,1,2, 2,2,3,2,2, 2,3,4, 8,8)
age <-  c("Adult",NA,NA,NA, "Adult",NA,NA,NA, "Adult",
 NA,"Adult",NA, NA,"Adult")
dat <- data.frame(id, year, age)
dat.id<-unique(dat$id)

#ID numbers for removal
bad<- data.frame(c(1,4))
names(bad)<-"id"
remove.value<-bad$id


good.id<- dat.id[!dat.id%in%remove.value]

#Combine all good ID numbers
if(exists("dat.2")){ rm(dat.2)}

for(i in good.id){
lala<-dat[which(dat$id==i),]

 if(!exists("dat.2")) {
  dat.2 <- lala } else {
  dat.2 <- rbind(dat.2, lala)
  }
}

Many thanks in advance for any suggestions



--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-records-from-a-large-dataframe-tp4646990.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Remove records from a large dataframe

2012-10-22 Thread penguins
Thanks Rui, your solution works great and is so fast!



--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-records-from-a-large-dataframe-tp4646990p4647029.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] box() doesnt work

2012-10-30 Thread penguins
Hi,

when plotting a graphic i find that the surrounding box disappears if I
adjust the margins with par(mar=..). Ive tried reassigning it with box() but
it doesnt seem to make any difference. Does anyone know a way to overcome
this?

Thanks in advance 



--
View this message in context: 
http://r.789695.n4.nabble.com/box-doesnt-work-tp4647909.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] conditional increase by increment

2012-07-18 Thread penguins
I am trying to assign increasing trip numbers to a binary variable ("land";
1=home and 0=away) where a string of 1's shouldn't increment the trip_no
more than once. 

For example; based on 
land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
the "trip_no" sequence produced should be   1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3

This is as far as I can get but Im stumped. In addition I need it to work on
data where the land variable can start on "0" or "1" for trip_no=1. Any help
would be hugely appreciated:

land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
trip_no <- rep(0, length(land))
gg<-cbind(land,trip_no)

increment <- function(x){
  eval.parent(substitute(x <- x + 1))
}

for(i in length(gg)){
  if(gg$land[[i]]==1) {
  gg$trip_no<-increment(trip_no[i])
}
 }

--
View this message in context: 
http://r.789695.n4.nabble.com/conditional-increase-by-increment-tp4636910.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] conditional increase by increment

2012-07-19 Thread penguins
Thanks William, that works fantastically!

I had a quick play with my data and have realised a potential problem in
that if an individual ends the series at home it records an additional
trip-no when one wasnt made. I was wondering whether you could think of a
way to alter it slightly so that the increment works on the 0 rather than
the 1;

for example:
land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
the "trip_no" sequence produced would be   1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3 

Many thanks

--
View this message in context: 
http://r.789695.n4.nabble.com/conditional-increase-by-increment-tp4636910p4637029.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] find date between two other dates

2012-08-05 Thread penguins
Hi,

I am trying to assign "Season" values to dates depending on when they occur.

For example, the following dates would be assigned the following "Season"
numbers based on the "season" intervals detailed below in the code:

ddate   Season
29/12/1998 20:00:33   1
02/01/1999 05:20:44   2
02/01/1999 06:18:36   2
02/02/1999 07:06:59   3
02/03/1999 07:10:56   4
02/03/1999 07:57:18   4

My approach so far doesnt work because of the time stamps and is probably
very long winded. However, to prevent errors I would prefer to keep the date
formats as dd/mm/ as oppose to a numeric format. Any help on the
following code would be gratefully recieved:
 
ddate <-  c("29/12/1998 20:00:33", "02/01/1999 05:20:44", "02/01/1999
06:18:36", "02/02/1999 07:06:59", "02/03/1999 07:10:56", "02/03/1999
07:57:18")
ddate <- as.POSIXct(strptime(ddate, "%d/%m/%Y %H:%M:%S"), "GMT")

is.between<-function(x, a, b) {
   (x > a) & (b > x)
}

ddate$s1 <- is.between(ddate, 01/12/1998 00:00:00, 31/12/1998 23:59:59)
ddate$s2 <- is.between(ddate, 01/01/1999 00:00:00, 31/01/1999 23:59:59)
ddate$s3 <- is.between(ddate, 01/02/1999 00:00:00, 28/02/1999 23:59:59)
ddate$s4 <- is.between(ddate, 01/03/1999 00:00:00, 31/03/1999 23:59:59)

Many thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/find-date-between-two-other-dates-tp4639231.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] find date between two other dates

2012-08-06 Thread penguins
Thanks arun and Rui; 3 fantastic suggestions. 

The Season interval is not always a month so arun's suggestion works better
for this dataset. I couldn't get the as.between function to work on arun's
second suggestion, it only returned NAs. 

However, arun's first suggestion worked a treat!

Many thanks 



--
View this message in context: 
http://r.789695.n4.nabble.com/find-date-between-two-other-dates-tp4639231p4639253.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] map axis on projected shapefiles

2012-08-08 Thread penguins
Hi,

I have overlayed 2 projected shapefiles using the "plot" function. 

When I plot them the numbers next to the x-axis ticks are printed in
scientific format (e.g. -4e+05, -3e+05,...,), and the problem is these dont
match up to the projected point locations I also wish to overlay from my
dataset. For example, according to the x-axis ticks the prime meridian (0°)
is in totally the wrong position.  

I have tried "degAxis" which only changes the axis to  4e+05°W, 3e+05°W,...,
and I cant seem to get map.grid to work.

Does anyone know why this would happen or what I have done wrong?


#Code is 
shelfline <- readOGR(dsn="C:\\ArcGIS\\phy_bathy_contours.shp", layer =
"phy_bathy_contours")
world <- readOGR(dsn="C:\\ArcGIS\\phy_coastline_plus_ssi_so.shp", layer =
"phy_coastline_plus_ssi_so")

plot(shelfline, axes=T,xlab=expression("Longitude"^o),
ylab=expression("Latitude"^o), yaxt="n", xaxt="n")
plot(world, col="lightslategray", add=T)

#attempts to alter axes
degAxis(1)
map.grid(col="black", labels=TRUE, pretty=TRUE, lty=2)

Many thanks





--
View this message in context: 
http://r.789695.n4.nabble.com/map-axis-on-projected-shapefiles-tp4639554.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] map axis on projected shapefiles

2012-08-08 Thread penguins
Since posting this I've figured out the problem was due to the shapefiles I
was reading in. As they were already projected they were in meters, as
oppose to decimal degrees. By opening the shapefiles in ArcGIS, reprojecting
them to WGS1984 and exporting, I could then reload them in R in decimal
degrees before reprojecting to my desired projection.

Thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/map-axis-on-projected-shapefiles-tp4639554p4639596.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] Remove several numbers from a sequence

2012-08-17 Thread penguins
Can anyone tell me how to remove several numbers for a sequence. For example:

xx<- c(1,5,7,10)
yy<-seq(1,10,1)

how do I get take xx away from yy to get the new sequence 

2,3,4,6,8,9

Many thanks in advance



--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Remove several numbers from a sequence

2012-08-20 Thread penguins
Hi!

Both yy[!(yy %in% xx)] and yy[is.na(match(yy,xx))] work a treat!

Developing this topic can anyone tell me how to extract rows which are not
replicated across 2 data frames. For example:

z1<-c(1,2,3,4)
z2<-c(5,6,7,8)
zz<- data.frame(cbind(z1,z2))

x1<-c(3,4)
x2<-c(7,8)
xx<- data.frame(cbind(x1,x2))

so the result would be:

1   5
2   6

Using setdiff(xx,zz) I can get the replicated rows but not the unique ones,
and setdiff(zz,xx) returns the all rows.

Many thanks







--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630p4640771.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Remove several numbers from a sequence

2012-08-20 Thread penguins
many thanks for lots of different solutions. I have a couple of  very large
data frames but using the unique identifier for each row and Arun's solution 

zz[is.na(match(zz$z1,xx$x1)),] 

has worked perfectly,

Thanks!



--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630p4640799.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.