我有两个csv文件,一个里头包含了source、target、weight等,另一个是id和label,前者的source和target都是用id来表示的。在将两个data.frame组合导出用于gephi制图的gexf文件时出现了:
Input is not proper UTF-8, indicate encoding !
Bytes: 0xB5 0xD2 0xC8 0xCA
Error: 1: Input is not proper UTF-8, indicate encoding !
Bytes: 0xB5 0xD2 0xC8 0xCA
我看了下,貌似是因为我的label都是中文汉字的原因。这个编码的问题我不是很懂,R也刚上手没多久,求指教。附上完整代码。
> library(igraph)> library(rgexf)> hero<- read.csv("C:/Users/12441/Desktop/hero.csv") %>% as.data.frame()> hero %>% head() Source Target Type id label timeset weight1 59 14 Undirected 1 NA NA 12 59 26 Undirected 2 NA NA 13 59 48 Undirected 3 NA NA 14 59 74 Undirected 4 NA NA 15 46 3 Undirected 5 NA NA 16 46 6 Undirected 6 NA NA 1> hero.tie <- subset(hero, select = c("Source", "Target", "weight"))> nrow(hero.tie)[1] 688> hero.name <- read.csv("C:/Users/12441/Desktop/heroname.csv") %>% as.data.frame() %>% subset(select = c("id", "label"))> hero.name %>% head() id label1 1 刘禅2 2 程咬金3 3 苏烈4 4 牛魔5 5 张飞6 6 庄周> hero.tie %>% head() Source Target weight1 59 14 12 59 26 13 59 48 14 59 74 15 46 3 16 46 6 1> summary(hero.tie$weight) Min. 1st Qu. Median Mean 3rd Qu. Max. 1 1 1 1 1 1 > nrow(hero.tie)[1] 688> g.hero <- graph.data.frame(hero.tie)> g.hero %>% as_data_frame() %>% head() from to weight1 59 14 12 59 26 13 59 48 14 59 74 15 46 3 16 46 6 1> class(g.hero)[1] "igraph"> hero.g <- as_data_frame(g.hero)[, c(1,2,3)]> colnames(hero.g) <- c("Source", "Target", "weight")> hero.g$Type <- "Undirected"> edges1 <- hero.g[c(1,2)]> hero.g %>% head() Source Target weight Type1 59 14 1 Undirected2 59 26 1 Undirected3 59 48 1 Undirected4 59 74 1 Undirected5 46 3 1 Undirected6 46 6 1 Undirected> nrow(hero.g)[1] 688> c(hero.g$Source, hero.g$Target) %>% unique() %>% length()[1] 74> id <- c(hero.g$Source, hero.g$Target) %>% unique()> match(id, hero.name$id) [1] 59 46 62 32 63 40 74 60 48 54 42 41 34 58 1 20 37 15 44 5 14 47 51 6 65[26] 69 56 2 52 11 66 22 24 64 36 38 25 67 55 72 53 23 29 35 28 61 4 39 21 30[51] 3 26 7 49 16 68 27 43 18 9 19 10 70 73 57 50 17 45 12 33 71 13 31 8> label <- hero.name$label[match(id, hero.name$id)]> nodes1 <- data.frame(id, label)> length(as_data_frame(g.hero)$weight)[1] 688> wt <- hero.g$weight> write.gexf(nodes = nodes1, edges = edges1, edgesWeight = wt, output = "C:/Users/12441/Desktop/herodta.gexf")Input is not proper UTF-8, indicate encoding !Bytes: 0xB5 0xD2 0xC8 0xCAError: 1: Input is not proper UTF-8, indicate encoding !Bytes: 0xB5 0xD2 0xC8 0xCA