Skip to content

simple problem, but not for me

2 messages · Rolf Turner, Spencer Graves

#
alexbri at netcabo.pt wrote:

            
Well, if you've deadened your brain by using Excel,
	no wonder.
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.
This would make B a ***vector*** equal to the concatenation
	of b1, ..., b5.

	Perhaps you mean:

		B <- cbind(b1,b2,b3,b4,b5)
You are very confused.  Your notation and your use of
	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
#
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: