[R] Data transformation
legen
legendy at gmail.com
Wed Nov 11 05:10:09 CET 2009
Thank you for your kind help. Your script works very well. Would you please
show me how to change NaN to zero and column variables 1, 2, ..., 8 to var1,
var2, ..., var8? Thanks again.
Legen
jholtman wrote:
>
> Is this what you want:
>
>> x <- read.table(textConnection("id code1 code2 p
> + 1 4 8 0.1
> + 1 5 7 0.9
> + 2 1 8 0.4
> + 2 6 2 0.2
> + 2 4 3 0.6
> + 3 5 6 0.7
> + 3 7 5 0.9"), header=TRUE)
>> closeAllConnections()
>> # create object like output from 'melt'
>> x.m <- data.frame(id=c(x$id, x$id), var=c(x$code1, x$code2),
> + variable=rep('p', 2*nrow(x)), value=c(x$p, x$p))
>> require(reshape) # use the reshape package
>> cast(x.m, id ~ var, mean)
> id 1 2 3 4 5 6 7 8
> 1 1 NaN NaN NaN 0.1 0.9 NaN 0.9 0.1
> 2 2 0.4 0.2 0.6 0.6 NaN 0.2 NaN 0.4
> 3 3 NaN NaN NaN NaN 0.8 0.7 0.9 NaN
>>
>
>
>
> On Tue, Nov 10, 2009 at 4:30 PM, legen <legendy at gmail.com> wrote:
>>
>> Dear all,
>>
>> I have a dataset as below:
>>
>> id code1 code2 p
>> 1 4 8 0.1
>> 1 5 7 0.9
>> 2 1 8 0.4
>> 2 6 2 0.2
>> 2 4 3 0.6
>> 3 5 6 0.7
>> 3 7 5 0.9
>>
>> I just want to rewrite it as this (vertical to horizontal):
>>
>> id var1 var2 var3 var4 var5 var6 var7 var8
>> 1 0 0 0 0.1 0.9 0 0.9 0.1
>> 2 0.4 0.2 0.6 0.6 0 0.2 0 0.4
>> 3 0 0 0 0 0.8 0.7 0.9 0
>>
>> For the third subject, there are two values being equal to 5 in code1 and
>> code2, but different values in p: 0.7 and 0.9, so I assigned their
>> average
>> 0.8 in var5.
>>
>> Does anybody can help me to handle this? Many thanks for your
>> consideration
>> and time.
>>
>> Legen
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Data-transformation-tp26291568p26291568.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
--
View this message in context: http://old.nabble.com/Data-transformation-tp26291568p26295766.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list