Dear R-experts,
recently, I started to discover the world of R. I came across a problem,
that I was unable to solve by myself (including searches in R-help, etc.)
I have a flat table similar to
key1 key2 value1
abcd_1 BP 10
abcd_1 BSMP 1A
abcd_1 PD 25
abcd_2 BP 20
abcd_3 BP 80
abcd_4 IA 30
abcd_4 PD 70
abcd_4 PS N
I wish to transform this table to obtain the following result:
key2
key1 BP BSMP IA PD PS
abcd_1 "10" "1A" "" "25" ""
abcd_2 "20" "" "" "" ""
abcd_3 "80" "" "" "" ""
abcd_4 "" "" "30" "70" "N"
I considered "table" and "xtabs" but I could not get the desired result: I
received cross-tables key1 vs. key2 that contained counts within the cells.
Can anybody help me?
Best wishes,
Christian
Dear R-experts,
recently, I started to discover the world of R. I came across a problem,
that I was unable to solve by myself (including searches in R-help, etc.)
I have a flat table similar to
key1 key2 value1
abcd_1 BP 10
abcd_1 BSMP 1A
abcd_1 PD 25
abcd_2 BP 20
abcd_3 BP 80
abcd_4 IA 30
abcd_4 PD 70
abcd_4 PS N
I wish to transform this table to obtain the following result:
key2
key1 BP BSMP IA PD PS
abcd_1 "10" "1A" "" "25" ""
abcd_2 "20" "" "" "" ""
abcd_3 "80" "" "" "" ""
abcd_4 "" "" "30" "70" "N"
I considered "table" and "xtabs" but I could not get the desired result: I
received cross-tables key1 vs. key2 that contained counts within the cells.
Can anybody help me?
Best wishes,
Christian
Dear R-experts,
recently, I started to discover the world of R. I came across a problem,
that I was unable to solve by myself (including searches in R-help, etc.)
I have a flat table similar to
key1 ? ?key2 ? ?value1
abcd_1 ?BP ? ? ?10
abcd_1 ?BSMP ? ?1A
abcd_1 ?PD ? ? ?25
abcd_2 ?BP ? ? ?20
abcd_3 ?BP ? ? ?80
abcd_4 ?IA ? ? ?30
abcd_4 ?PD ? ? ?70
abcd_4 ?PS ? ? ?N
I wish to transform this table to obtain the following result:
? ? ? ?key2
key1 ? ?BP ? ? ?BSMP ? ?IA ? ? ?PD ? ? ?PS
abcd_1 ?"10" ? ?"1A" ? ?"" ? ? ?"25" ? ?""
abcd_2 ?"20" ? ?"" ? ? ?"" ? ? ?"" ? ? ?""
abcd_3 ?"80" ? ?"" ? ? ?"" ? ? ?"" ? ? ?""
abcd_4 ?"" ? ? ?"" ? ? ?"30" ? ?"70" ? ?"N"
I think we would say that a dataframe of the first type is in the
"long" format, while the other one you want is in the "wide" format.
I've done changes like that with the "reshape" function that is in the
stats package.
This example you propose is like making one column for each "country"
where key 1 is like the "year" in which the observation is made.
Right?
You don't have an easily cut-and-pasteable code example, so I've
generated a little working example. Here, x1 is key 1 and x2 is key 2.
Dear R-experts,
recently, I started to discover the world of R. I came across a problem,
that I was unable to solve by myself (including searches in R-help, etc.)
I have a flat table similar to
key1 ? ?key2 ? ?value1
abcd_1 ?BP ? ? ?10
abcd_1 ?BSMP ? ?1A
abcd_1 ?PD ? ? ?25
abcd_2 ?BP ? ? ?20
abcd_3 ?BP ? ? ?80
abcd_4 ?IA ? ? ?30
abcd_4 ?PD ? ? ?70
abcd_4 ?PS ? ? ?N
I wish to transform this table to obtain the following result:
? ? ? ?key2
key1 ? ?BP ? ? ?BSMP ? ?IA ? ? ?PD ? ? ?PS
abcd_1 ?"10" ? ?"1A" ? ?"" ? ? ?"25" ? ?""
abcd_2 ?"20" ? ?"" ? ? ?"" ? ? ?"" ? ? ?""
abcd_3 ?"80" ? ?"" ? ? ?"" ? ? ?"" ? ? ?""
abcd_4 ?"" ? ? ?"" ? ? ?"30" ? ?"70" ? ?"N"
I considered "table" and "xtabs" but I could not get the desired result: I
received cross-tables key1 vs. key2 that contained counts within the cells.
Can anybody help me?
With the reshape package:
cast(mydf, key1 ~ key2)
You can find out more at http://had.co.nz/reshape
Hadley