Peter Dalgaard [EMAIL PROTECTED] writes:
I think I'd go for something like
V2 - c(4, 6, 10)[factor(V1, levels=c(10, 20, 30))]
Come to think of it:
c(4, 6, 10)[match(V1, c(10, 20, 30))]
is more to the point.
--
O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
Hi,
How can I accomplish this task in R?
V1
10
20
30
10
10
20
Create a new column V2 such that:
If V1 = 10 then V2 = 4
If V1 = 20 then V2 = 6
V1 = 30 then V2 = 10
So the O/P looks like this
V1 V2
10 4
20 6
30 10
Try:
V1 - matrix(c(10, 20, 30, 10, 10, 20), nc = 1)
V2 - 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30)
or
V2 - matrix(c(4, 6, 10)[V1/10], nc = 1)
On 4/21/06, Sachin J [EMAIL PROTECTED] wrote:
Hi,
How can I accomplish this task in R?
V1
10
20
30
10
10
20
Hi Gabor,
The first one works fine. Just out of curiosity, in second solution: I dont
want to create a matrix. I want to add a new column to the existing dataframe
(i.e. V2 based on the values in V1). Is there a way to do it?
TIA
Sachin
Gabor Grothendieck [EMAIL PROTECTED]
DF - data.frame(V1 = c(10, 20, 30, 10, 10, 20))
DF$V2 - with(DF, 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30))
DF$V3 - c(4, 6, 10)[DF$V1/10]
or
DF - data.frame(V1 = c(10, 20, 30, 10, 10, 20))
DF - transform(DF, V2 = 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30),
V3 = c(4, 6,
On 4/21/2006 4:05 PM, Sachin J wrote:
Hi,
How can I accomplish this task in R?
V1
10
20
30
10
10
20
Create a new column V2 such that:
If V1 = 10 then V2 = 4
If V1 = 20 then V2 = 6
V1 = 30 then V2 = 10
Gabor's solution is fine;
Duncan Murdoch [EMAIL PROTECTED] writes:
On 4/21/2006 4:05 PM, Sachin J wrote:
Hi,
How can I accomplish this task in R?
V1
10
20
30
10
10
20
Create a new column V2 such that:
If V1 = 10 then V2 = 4
If V1 = 20 then
Duncan Murdoch wrote:
On 4/21/2006 4:05 PM, Sachin J wrote:
Hi,
How can I accomplish this task in R?
V1
10
20
30
10
10
20
Create a new column V2 such that:
If V1 = 10 then V2 = 4
If V1 = 20 then V2 = 6
V1 = 30 then V2 = 10
Gabor's
Here is a compact solution using approx:
DF$V2 - approx(c(10, 20, 30), c(4,6,10), DF$V1)$y
On 4/21/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
DF - data.frame(V1 = c(10, 20, 30, 10, 10, 20))
DF$V2 - with(DF, 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30))
DF$V3 - c(4, 6,