In R, how to select a year-specific datum from multiple year columns? -


i have dataset in each column, want select response datum based year of observation. version of dataset has first 4 columns; want create fifth column using r syntax. in fifth column, yr.response, datum corresponds correct year. i'm considering iteration if ifelse() statements (e.g. if year==2000, select column y2000 etc), can't work. should add actual dataset has thirty years , 19k rows, want able automate.

year<-c(2000,2000,2001,2002) y2000<-c(65,43,42,74) y2001<-c(98,93,987,948) y2002<-c(875,983,776,736) yr.response<-c(65,43,987,736) x<-data.frame(cbind(year, y2000,y2001,y2002,yr.response)) 

my.df <- data.frame(year, y2000, y2001, y2002) rownames(my.df) <- as.character(rownames(my.df)) my.df$yr.respnose <- my.df[cbind(rownames(my.df), paste0("y", my.df$year))] 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -