我是新來的。我剛開始學習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
那是,第一列(名稱)中的條目將變得唯一,第二列(顏色)中的級別將成爲新列,並且這些新列中的條目將來自對應在原始數據框中的第三列(值)中的行。
我可以使用下面的做到這一點:
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
但是,如果有很多的顏色欄的水平,這將不是很理想。我將如何繼續這樣做?
謝謝!
這是快。謝謝! – chowching