简体   繁体   中英

Subtract multiple columns by one column

I want to subtract the year in which the respondents were born (variables containing yrbrn) by the variable for year of the interview (inwyys) and save the results as new variables in the data frame.

Head of the data frame:

   inwyys  yrbrn2  yrbrn3  yrbrn4  yrbrn5  yrbrn6  yrbrn7  yrbrn8
1   2012    1949    1955     NA      NA      NA      NA       NA
2   2012    1983    1951    1956    1989    1995    2003     2005
3   2012    1946    1946    1978     NA      NA      NA       NA 
4   2013     NA      NA      NA      NA      NA      NA       NA
5   2013    1953    1959    1980    1985    1991    2008     2011
6   2013    1938     NA      NA      NA      NA      NA       NA

Can someone help me with that?

Thank you very much!

This can be done by sub-setting ( x[,-1] ..take everything but not the first column, x[,1] ..take the first column) your data and make the subtraction. With cbind you can bind the new result to the original data.

cbind(x, x[,-1] - x[,1])
#  inwyys yrbrn2 yrbrn3 yrbrn4 yrbrn5 yrbrn6 yrbrn7 yrbrn8 yrbrn2 yrbrn3 yrbrn4 yrbrn5 yrbrn6 yrbrn7 yrbrn8
#1   2012   1949   1955     NA     NA     NA     NA     NA    -63    -57     NA     NA     NA     NA     NA
#2   2012   1983   1951   1956   1989   1995   2003   2005    -29    -61    -56    -23    -17     -9     -7
#3   2012   1946   1946   1978     NA     NA     NA     NA    -66    -66    -34     NA     NA     NA     NA
#4   2013     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA
#5   2013   1953   1959   1980   1985   1991   2008   2011    -60    -54    -33    -28    -22     -5     -2
#6   2013   1938     NA     NA     NA     NA     NA     NA    -75     NA     NA     NA     NA     NA     NA

Data:

x  <- read.table(header=TRUE, text="   inwyys  yrbrn2  yrbrn3  yrbrn4  yrbrn5  yrbrn6  yrbrn7  yrbrn8
1   2012    1949    1955     NA      NA      NA      NA       NA
2   2012    1983    1951    1956    1989    1995    2003     2005
3   2012    1946    1946    1978     NA      NA      NA       NA 
4   2013     NA      NA      NA      NA      NA      NA       NA
5   2013    1953    1959    1980    1985    1991    2008     2011
6   2013    1938     NA      NA      NA      NA      NA       NA")

I believe the following is what you are looking for

data$newvar1<-data$yrbrn2-data$inwyys

But replace "data" with the name of your data set. If you want to do it for each yrbrn column, just change "newvar1" to "newvar2" etc so you do not override your previous calculations

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