mardi 20 septembre 2016

How to use ifelse and paste functions

I am learning the use of ifelse function from Zuur et al (2009) A Beginners guide to R. In one exercise, there is a data frame called Owls which contains data about about 27 nests and two night of observations.

structure(list(Nest = structure(c(1L, 1L, 1L, 1L), .Label = "AutavauxTV", class = "factor"), FoodTreatment = structure(c(1L, 2L, 1L, 1L), .Label = c("Deprived", "Satiated"), class = "factor"), SexParent = structure(c(1L, 1L, 1L, 1L), .Label = "Male", class = "factor"), ArrivalTime = c(22.25, 22.38, 22.53, 22.56), SiblingNegotiation = c(4L, 0L, 2L, 2L), BroodSize = c(5L, 5L, 5L, 5L), NegPerChick = c(0.8, 0, 0.4, 0.4)), .Names = c("Nest", "FoodTreatment", "SexParent", "ArrivalTime", "SiblingNegotiation", "BroodSize", "NegPerChick" ), row.names = c(NA, 4L), class = "data.frame")

The two nights differed as to the feeding regime (satiated or deprived) and are indicated in the Foodregime variable. The task is to use ifelse and past functions that make a new categorical variable that defines observations from a single night at a particular nest.

In the solutions the following code is suggested: Owls <- read.table(file = "Owls.txt", header = TRUE, dec = ".") ifelse(Owls$FoodTreatment == "Satiated", Owls$NestNight <- paste(Owls$Nest, "1",sep = ""), Owls$NestNight <- paste(Owls$Nest, "2",sep = ""))

add apperently it creates a new variable with values the endings of which vary ("-1" or "-2")

Like this

however when I call the original dataframe, all values in NestNight varibale end to "-2."

LIKE THIS

What is the problem here? Did the authors missed something from the code or it's me who is not getting it?

Many thanks

EDIT: Sorry, I wanted to give a reproducible example by copying my data using dput but it did not work. If you can let me know how I can correct it so that it appears properly, I'd be greatful too!

Aucun commentaire:

Enregistrer un commentaire