# [R] simple problem, but not for me

Spencer Graves spencer.graves at pdf.com
Sat Mar 19 19:56:52 CET 2005

```     The suggestions by Uwe and Rolf use some of the subtler features of
R.  A simpler (to me) if more tedious approach is provided by the
following:

Data <- data.frame(a1=1:4, a2=5:8, a3=9:12)

Data\$b1 <- Data\$a1/(Data\$a1+Data\$a2+Data\$a3)
Data\$b2 <- Data\$a2/(Data\$a2+Data\$a3)

> Data
a1 a2 a3         b1        b2
1  1  5  9 0.06666667 0.3571429
2  2  6 10 0.11111111 0.3750000
3  3  7 11 0.14285714 0.3888889
4  4  8 12 0.16666667 0.4000000

The above can be written in fewer characters using "with":

Data\$b1 <- with(Data, a1/(a1+a2+a3))
Data\$b2 <- with(Data, a2/(a2+a3))

If you want something that will work with an arbitrary number of
columns, consider the following:

Data <- data.frame(a1=1:4, a2=5:8, a3=9:12)
dat2 <- Data
k <- length(dat2)
for(i in (k-1):1)
dat2[[i]] <- (dat2[[i]]+dat2[[i+1]])
Dat2 <- cbind(Data, Data/dat2)
names(Dat2)[k+(1:k)] <- paste("b", 1:k, sep="")

> Data
a1 a2 a3
1  1  5  9
2  2  6 10
3  3  7 11
4  4  8 12
> dat2
a1 a2 a3
1 15 14  9
2 18 16 10
3 21 18 11
4 24 20 12
> Dat2
a1 a2 a3         b1        b2 b3
1  1  5  9 0.06666667 0.3571429  1
2  2  6 10 0.11111111 0.3750000  1
3  3  7 11 0.14285714 0.3888889  1
4  4  8 12 0.16666667 0.4000000  1

If you want to do this more than once, you can wrap the above code
in a function;  see "Writing your own functions" in "An Introduction to
R", available, e.g., vial help.start().

hope this helps.
spencer graves
p.s.  The posting guide "http://www.R-project.org/posting-guide.html"
also seems quite valuable.  In an discussion on (and off) this list
earlier this week, several people reported they had found solutions to
their own problems in the process of preparing a question to post to
this list.  And even if you don't find a solution, the result will more
likely elicit useful replies.

Rolf Turner wrote:

>alexbri at netcabo.pt wrote:
>
>
>
>>Hello, I'm new in R and I want to do one thing that is very easy in
>>excel, however, I cant do it in R.
>>
>>
>
>	no wonder.
>
>
>
>>Suppose we have the data frame:
>>
>>data<- data.frame(A=c("a1","a2","a3","a4","a5"))
>>
>>
>
>	Oh, for Pete's sake!  This makes ``data'' (NOT a good name
>	for an object!) into a data frame with a single column named
>	``A''.  That column will be a factor with 5 entries (an 5
>	levels) with these levels being (the character strings)
>	"a1","a2","a3","a4", and "a5".
>
>	Nothing to do with what you actually want.
>
>
>
>>I need to obtain another column in the same data frame (lets say
>>B=c(b1,b2,b3,b4,b5) in the following way:
>>
>>
>
>	This would make B a ***vector*** equal to the concatenation
>	of b1, ..., b5.
>
>	Perhaps you mean:
>
>		B <- cbind(b1,b2,b3,b4,b5)
>
>
>
>>b1=a1/(a1+a2+a3+a4+a5)
>>
>>b2=a2/(a2+a3+a4+a5)
>>
>>b3=a3/(a3+a4+a5)
>>
>>b4=a4/(a4+a5)
>>
>>b5=a5/a5
>>
>>a1..a5 and b1...b5 are always numeric values
>>
>>(this is just an example, what I really want is apply this kind of
>>formula to a much larger data frame)
>>
>>
>>
>	the function c() are all wrong.
>
>	If you are going to use R, get the basic syntax straight.
>
>	You probably should be using matrices rather than data frames
>	given that the entries are all numeric.
>
>	Be that as it may, if A is a (numeric) matrix then
>
>		B <- A/t(apply(A,1,function(x){rev(cumsum(x))}))
>
>	will give what you appear to want.
>
>				cheers,
>
>					Rolf Turner
>					rolf at math.unb.ca
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help