看板 R_Language 關於我們 聯絡資訊
大概寫了一下。 最後 res 是 species by locations 的 binary matrix 為所求。 重點是在要把所有的地點集中起來再逐列與各spp出現的地點做比對。 library(magrittr) ## fatch data con <- url( "http://archive.ics.uci.edu/ml/machine-learning-databases/plants/plants.data", "r" ) src <- readLines(con) close(con) ## split string by comma srcLine <- strsplit(src, ",") ## get sp name sppName <- lapply(srcLine, function(x) { x[1] }) %>% unlist ## get location name per sp locName <- lapply(srcLine, function(x) { x[-1] }) ## get all unique location name locNameUni <- locName %>% unlist %>% unique ## binary matrix res <- lapply(locName, function(x) { locNameUni %in% x }) %>% do.call("rbind", .) %>% set_colnames(locNameUni) %>% set_rownames(sppName) ※ 引述《genius888053 (少年YO)》之銘言: : https://i.imgur.com/InHBPvq.jpg
: 我複製到記事本後用txt檔案方式讀取可是卻發生以下問題 : https://i.imgur.com/A7iKdyy.jpg
: 上圖我有試著將sep刪除掉 可是一樣發生上述情形 : https://i.imgur.com/dnBtUX7.jpg
: 儘管我讀出來後卻發生資料不整齊和變數只有一筆的情形 : 求板上大大提點 : 第一次發文,有誤請見諒 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.246.95.32 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1528474412.A.814.html ※ 編輯: andrew43 (111.246.95.32), 06/09/2018 00:14:48 ※ 編輯: andrew43 (111.246.95.32), 06/09/2018 00:15:35
andrew43: 我後來猜想你應該比較可能使用 locations by species 06/09 00:52
andrew43: 需要的話再發個聲吧 06/09 00:53
andrew43: 或是自己先試試看 t(res) 是不是你要的 06/09 00:54
genius888053: 太感謝大大了 我等等來試看看 06/09 01:45