Re: [問題] 求解此檔案如何讀入和如何跑出分群

看板R_Language作者 (討厭有好心推文後刪文者)時間7年前 (2018/06/09 00:13), 7年前編輯推噓1(103)
留言4則, 2人參與, 7年前最新討論串2/3 (看更多)
大概寫了一下。 最後 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

06/09 00:52, 7年前 , 1F
我後來猜想你應該比較可能使用 locations by species
06/09 00:52, 1F

06/09 00:53, 7年前 , 2F
需要的話再發個聲吧
06/09 00:53, 2F

06/09 00:54, 7年前 , 3F
或是自己先試試看 t(res) 是不是你要的
06/09 00:54, 3F

06/09 01:45, 7年前 , 4F
太感謝大大了 我等等來試看看
06/09 01:45, 4F
文章代碼(AID): #1R6giiWK (R_Language)
文章代碼(AID): #1R6giiWK (R_Language)