简体   繁体   中英

Assigning column of data.frame in 'with' call

Is there an elegant way of assigning a column in a data.frame in a call to 'with'. For example I'd like to do something like this:

> df <- data.frame(x=runif(5), y=runif(5))
> df
          x         y
1 0.4010225 0.1702563
2 0.7204338 0.9929117
3 0.3755553 0.9229862
4 0.2959035 0.3947730
5 0.3273934 0.3680618
> with(df, y <- cumsum(x))
> df
          x         y
1 0.4010225 0.1702563
2 0.7204338 0.9929117
3 0.3755553 0.9229862
4 0.2959035 0.3947730
5 0.3273934 0.3680618

But in the end df$y would be the cumulative sum of df$x. I know that df$y <- cumsum(df$x) would work well for such a trivial example, but as things get more complicated it would be simpler to have something like the above. Is there a good solution for that?


Editing to specify multi-column calculations.

Is there any way to do a multi-column calculations from within without having to restate the data.frame name? eg

> df <- data.frame(a=runif(5), b=runif(5), c=runif(5))
> df
          a          b          c
1 0.4470582 0.57788300 0.06578355
2 0.6694826 0.13988892 0.80460530
3 0.7300993 0.78663734 0.68503691
4 0.6825741 0.07943231 0.02903320
5 0.6384533 0.42809113 0.26117171
> within(df, { x <- cumsum(a); y <- rowSums(df[,1:2]) })
          a          b          c         y         x
1 0.4470582 0.57788300 0.06578355 1.0249412 0.4470582
2 0.6694826 0.13988892 0.80460530 0.8093715 1.1165408
3 0.7300993 0.78663734 0.68503691 1.5167366 1.8466401
4 0.6825741 0.07943231 0.02903320 0.7620064 2.5292142
5 0.6384533 0.42809113 0.26117171 1.0665445 3.1676675

You can use transform or within . In both cases you will have to assign the result back to df if you want it to persist.

> transform(df, y=cumsum(x))
          x         y
1 0.7430507 0.7430507
2 0.2858004 1.0288512
3 0.9565152 1.9853664
4 0.4379119 2.4232783
5 0.6885749 3.1118532
> within(df, y <- cumsum(x))
          x         y
1 0.7430507 0.7430507
2 0.2858004 1.0288512
3 0.9565152 1.9853664
4 0.4379119 2.4232783
5 0.6885749 3.1118532

Here is a more complicated example that actually modifies df :

df <- within(df, {z <- x * 2; y <- cumsum(z); rm(z)})

Note we rm(z) as otherwise that would add another column to df .

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