loop for adding columns

13 views
Skip to first unread message

Simon Givoli

unread,
Oct 3, 2015, 8:46:43 AM10/3/15
to Israel R User Group

Hi all,

 

I have a df that looks basically like this:

 

Id   A    B    C  total

3    5    0    1    6

4    5    4    3   12

5    3    2    4    9

 

I want to divide each cell in variables A-C with the total and add the quotient in new columns, like this:

 

Id   A    B    C  total prop_a      prop_b      prop_c

3    5    0    1    6

4    5    4    3   12

5    3    2    4    9

 

I wrote the following code:

 

for (i in 5:7){

  j = 2 #starting counter

  df$i_prop <- cbind(df$"[j]"/df$total)

  j = j + 1

}

 

And got this message:

Error in `$<-.data.frame`(`*tmp*`, "i_prop", value = numeric(0)) :

  replacement has 0 rows, data has 126

 

 

Can you guys help? I don't understand the error and how to fix it (sorry, still newbie)

Thanks,

Simon

amit gal

unread,
Oct 3, 2015, 8:58:50 AM10/3/15
to israel-r-...@googlegroups.com
from dplyr, you can use mutate to manually create columns:
new.df = df %>% mutate(prop_a = A/total, prop_b = B/total, prop_c = C/total)

if you want to be more generic:

df.prop = df[2:4]/df$total

names(df.prop) = paste("prop",names(df)[2:4],sep="_")
df.all = cbind(df,df.prop)



--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Givoli

unread,
Oct 3, 2015, 11:34:12 AM10/3/15
to israel-r-...@googlegroups.com

Thanks Amit.


The dplyr solution works, but I have 51 columns to compute  – Is there a way to write this command without repeating the division 51 times?

 


BTY – The generic solution doesn't work, still trying to see why.


Simon


סיימון גבעולי
פסיכולוג תעסוקתי 
052-3626345

amit gal

unread,
Oct 3, 2015, 11:52:47 AM10/3/15
to israel-r-...@googlegroups.com

The generic way should work for you. Maybe you shoulf convert to a matrix first. Will check later

Yoni Sidi

unread,
Oct 3, 2015, 11:52:50 AM10/3/15
to Israel R User Group
use mutate_each(funs(./total)) this will apply to all columns the function. if you want to use specific columns use either select or contains.

use the linked pdf as a guide for the select or contains and in general for dplyr and tidyr

https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf

Simon Givoli

unread,
Oct 3, 2015, 11:56:07 AM10/3/15
to israel-r-...@googlegroups.com

The generic works – had a small typo…

thanks for all the advice!


Simon


סיימון גבעולי
פסיכולוג תעסוקתי 
052-3626345

Reply all
Reply to author
Forward
0 new messages