看板 R_Language 關於我們 聯絡資訊
各位高手大家好! [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題描述] 主要是手邊的data要作格式轉換 例如 id x1_1 x1_2 x2_1 x2_2 x3_1 x3_2 x4_1 x4_2 1 1 2 1 1 2 2 1 2 2 1 1 2 1 1 2 2 2 3 1 1 1 2 1 1 2 1 ... 500 1 1 1 1 2 2 2 2 格式想轉變成 id x1 x2 x3 x4 1 1/2 1/1 2/2 1/2 2 1/1 2/1 1/2 2/2 3 1/1 1/2 1/1 2/1 ... 500 1/1 1/1 2/2 2/2 以上,小弟在此先謝謝各位高手 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.76.175.169 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1426589397.A.1FA.html
celestialgod: mutate_(df, .dots = paste0("x", 1:4, "_", 1, 03/17 19:09
celestialgod: "+x", 1:4, "_", 2)) 03/17 19:09
celestialgod: df是你的data.frame 03/17 19:09
celestialgod: mutate_ 先library(dplyr) 03/17 19:10
celestialgod: mutate_(x, .dots = paste0("paste(x", 1:4, 03/17 19:11
celestialgod: "_", 1, ",x", 1:4, "_", 2, ",sep='/')")) 03/17 19:11
celestialgod: x是你的data.frame 03/17 19:11
celestialgod: 前三推打太快,忘記改成你要你的格式,抱歉 03/17 19:12
celestialgod: mutate_之後 再重新命名跟選你需要的column出來即可 03/17 19:12
swedrf0112: t(apply(data,1,function(x) 03/17 19:13
swedrf0112: paste0(x[seq(1,length(x),2)],"/" 03/17 19:13
swedrf0112: ,x[seq(2,length(x),2)]))) 03/17 19:14
swedrf0112: data放你的資料~~ 03/17 19:15
celestialgod: s大,第一個seq應該從3開始,而非1,第一個是id。 03/17 19:24
swedrf0112: 3Q!! 沒看到 修改一下 第一個seq(2,length(x),2) 03/17 19:33
swedrf0112: 第二個seq(3,length(x),2) 03/17 19:33
andrew43: 親愛的板友,如果要推多次連續code,建議直接回文喔。 03/17 20:41
yaohwei: 謝謝各位,成功了 03/18 08:53