简体   繁体   中英

Fill specific values in a data frame using another column as an index

I have a data frame that looks like this:

 species<-"ABC" ind<-rep(1:4,each=24) hour<-rep(seq(0,23,by=1),4) month<-rep(seq(1,12),8) depth<-runif(length(ind),1,50) df<-data.frame(species,ind,month,hour,depth) 

What I would like is to use the column month to specify intervals for each season and return those values in a new column from the same data frame. I was using this code for the seasons, which seems to work fine,

 # Classify months into seasons summer<-c(1,2,12) fall<-c(3,4,5) winter<-c(6,7,8) spring<-c(9,10,11) # Create a new column with seasons df$season<-NA for(i in 1:nrow(df)){ if(df$month[i]%in%summer){df$season[i]<-"1-summer"} else if(df$month[i]%in%fall){df$season[i]<-"2-fall"} else if(df$month[i]%in%winter){df$season[i]<-"3-winter"} else if(df$month[i]%in%spring){df$season[i]<-"spring"} 

}

However, this loop in already inside of a bigger loop with more complex and bigger data bases. So I was looking for a faster, more efficient approach. The reason that I am using a loop rather than cutting or subsetting my original data frame is because the first loop that I am using is separating and performing analyses on individual animals. The length of resulting data frame varies between animals and one of the problems that I was having is that not all animals were present in all months, so when I was trying to assign seasons inside the loop for animals that were not present on a particular season, then R gave me an error message...

seasons <- c("1-summer", "2-fall", "3-winter", "spring")
df$season2 <- factor(trunc(df$month %% 12 / 3) + 1, labels = seasons)
table(df$season, df$season2)

You can convert df$season2 to character if you wish.

I'd just generate the lookup table for season names and apply that:

> season.names <- rep("",12)
> season.names[summer] <- "1-summer"
> season.names[fall] <- "2-fall"
> season.names[winter] <- "3-winter"
> season.names[spring] <- "4-spring"
> season.names
 [1] "1-summer" "1-summer" "2-fall"   "2-fall"   "2-fall"   "3-winter" "3-winter"
 [8] "3-winter" "4-spring" "4-spring" "4-spring" "1-summer"
> df$season <- season.names[df$month]
> head(df)
  species ind month hour     depth   season
1     ABC   1     1    0 41.643471 1-summer
2     ABC   1     2    1 36.055533 1-summer
3     ABC   1     3    2  1.901639   2-fall
4     ABC   1     4    3  7.737539   2-fall
5     ABC   1     5    4 35.327364   2-fall
6     ABC   1     6    5  9.156978 3-winter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM