看板 R_Language 關於我們 聯絡資訊
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) 效能諮詢(我想讓R 跑更快) [軟體熟悉度]: *[1;30;40m 新手(沒寫過程式,R 是我的第一次) [問題敘述]: 請簡略描述你所要做的事情,或是這個程式的目的 所附的程式碼有成功將上面的格式轉為下面的格式 但想請教的是dylpr或R的語法中有沒有更簡潔撰寫的方式呢? name sex score1 score2 score3 1 sally f 100 23 24 2 tony m 52 32 45 3 sam m 65 42 34 轉成 name sex score value 1 sally f score1 100 2 sally f score2 23 3 sally f score3 24 4 tony m score1 52 5 tony m score2 32 6 tony m score3 45 7 sam m score1 65 8 sam m score2 42 9 sam m score3 34 [程式範例]: d1 <- data.frame(d[1:2]) d1 <- d1[rep(seq_len(nrow(d1)), each=3)] col <- str_pad(c(1:3),2,"left",'0') d1 <- mutate(d1, SCORE = rep(col,length(d[[1]]))) d2 <- data.frame(t(d[3:5]),stringsAsFactors = FALSE) temp <- stack(d2) temp[2] <- NULL d1 <- cbind(d1,temp) [環境敘述]: win7 32bit 4G R 3.1.2 [關鍵字]: Column to Row, dplyr -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.195.74.150 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1443706294.A.C6B.html
andrew43: 用 melt() 大概是最常看到的方法。 10/01 21:34
andrew43: 可用「wide to long data table R」當關鍵字。 10/01 21:36
celestialgod: tidyr才有 dplyr沒有 10/01 21:49
celestialgod: gather 10/01 21:50
celestialgod: gather(d1, score, value, -name, - sex) 10/01 22:02
andrew43: 喔好像是經緯度。as.factor(paste(經度, 緯度)) 10/01 23:19
andrew43: 推錯 10/01 23:20