2016-08-03 8 views
1

ここは新しいです。私はちょうど私がこの質問を持っているR.列エントリを列に変換するR

を学び始めました:

name = c("John", "John","John","John","Mark","Mark","Mark","Mark","Dave", "Dave","Dave","Dave") 
color = c("red", "blue", "green", "yellow","red", "blue", "green", "yellow","red", "blue", "green", "yellow") 
value = c(1,2,1,3,5,5,3,2,4,6,7,8) 
df = data.frame(name, color, value) 
#View(df) 
df 
#  name color value 
# 1 John red  1 
# 2 John blue  2 
# 3 John green  1 
# 4 John yellow  3 
# 5 Mark red  5 
# 6 Mark blue  5 
# 7 Mark green  3 
# 8 Mark yellow  2 
# 9 Dave red  4 
# 10 Dave blue  6 
# 11 Dave green  7 
# 12 Dave yellow  8 

と私はそれが次のようになりたい::

私はデータフレームを持っていると仮定し

# names red blue green yellow 
#1 John 1 2  1  3 
#2 Mark 5 5  3  2 
#3 Dave 4 6  7  8 

ことは、最初の列(名前)のエントリは一意になり、2番目の列(色)のレベルは新しい列になり、これらの新しい列に入るエントリは対応する列元のデータフレームの3番目の列(値)に行を追加します。

私は、次を使用してこれを実現することができます

library(dplyr) 
    df = df %>% 
    group_by(name) %>% 
    mutate(red = ifelse(color == "red", value, 0.0), 
     blue = ifelse(color == "blue", value, 0.0), 
     green = ifelse(color == "green", value, 0.0), 
     yellow = ifelse(color == "yellow", value, 0.0)) %>% 
    group_by(name) %>% 
    summarise_each(funs(sum), red, blue, green, yellow) 
df 
    name red blue green yellow 
1 Dave  4  6  7  8 
2 John  1  2  1  3 
3 Mark  5  5  3  2 

しかし、レベルの多くは、色の列に存在する場合、これは理想的ではないでしょう。私はそれをどうやってやりますか?

ありがとうございました!

答えて

3

OPは、パッケージのdplyrファミリを使用すると、素敵なオプションは、我々は秩序を維持するために%>%

library(dplyr) 
df %>% 
    spread(color, value) 

を使用する必要がある場合、我々はできるtidyr

library(tidyr) 
spread(df, color, value) 
# name blue green red yellow 
#1 Dave 6  7 4  8 
#2 John 2  1 1  3 
#3 Mark 5  3 5  2 

であります'color'をfactorクラスに変換し、levelsuniqueの値として 'color'と指定してから電子spread

df %>% 
    mutate(color = factor(color, levels = unique(color))) %>% 
    spread(color, value) 
# name red blue green yellow 
#1 Dave 4 6  7  8 
#2 John 1 2  1  3 
#3 Mark 5 5  3  2 

それともdcastより高速でdata.tableを使用することができます。 data.tableに変換し、dcastdata.tableから使用することには利点があります。それはdcastからreshape2までずっとずっと速いです。

library(data.table) 
dcast(setDT(df), name~color, value.var="value") 
# name blue green red yellow 
#1: Dave 6  7 4  8 
#2: John 2  1 1  3 
#3: Mark 5  3 5  2 

注:両方のソリューションでは、我々が期待される出力のように列名を取得し、BTW変更することができる(それに接続されているすべての醜い接尾辞または接頭辞を持っていますが、それは、コードの別の行ではありません)


我々はbase Rが必要な場合は、1つのオプションですから、その後、クロスタブを望むtapply

with(df, tapply(value, list(name, color), FUN = I)) 
#  blue green red yellow 
#Dave 6  7 4  8 
#John 2  1 1  3 
#Mark 5  3 5  2 
+1

からからdcastを使用することができます。ありがとうございました! – chowching

3

のですか?

> xtabs(value~name+color, df) 
     color 
name blue green red yellow 
    Dave 6  7 4  8 
    John 2  1 1  3 
    Mark 5  3 5  2 
3

あなたはreshape2パッケージ

library(reshape2) 
dcast(df, name~color) 


# name blue green red yellow 
#1 Dave 6  7 4  8 
#2 John 2  1 1  3 
#3 Mark 5  3 5  2 

それとも他のことができます。reshape速かったbase R

reshape(df, idvar="name", timevar="color", direction="wide") 


# name value.red value.blue value.green value.yellow 
#1 John   1   2   1   3 
#5 Mark   5   5   3   2 
#9 Dave   4   6   7   8 
関連する問題