全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
2750 4
2017-12-15
我有两个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
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-12-15 15:28:42
好像乱码了...我冲发一下完整代码

> library(igraph)
> library(rgexf)
> hero<- read.csv("C:/Users/12441/Desktop/hero.csv") %>% as.data.frame()
> hero %>% head()
  Source Target       Type id label timeset weight
1     59     14 Undirected  1    NA      NA      1
2     59     26 Undirected  2    NA      NA      1
3     59     48 Undirected  3    NA      NA      1
4     59     74 Undirected  4    NA      NA      1
5     46      3 Undirected  5    NA      NA      1
6     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  label
1  1   刘禅
2  2 程咬金
3  3   苏烈
4  4   牛魔
5  5   张飞
6  6   庄周
> hero.tie %>% head()
  Source Target weight
1     59     14      1
2     59     26      1
3     59     48      1
4     59     74      1
5     46      3      1
6     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 weight
1   59 14      1
2   59 26      1
3   59 48      1
4   59 74      1
5   46  3      1
6   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       Type
1     59     14      1 Undirected
2     59     26      1 Undirected
3     59     48      1 Undirected
4     59     74      1 Undirected
5     46      3      1 Undirected
6     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 0xCA
Error: 1: Input is not proper UTF-8, indicate encoding !
Bytes: 0xB5 0xD2 0xC8 0xCA
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-15 15:38:15
输入的编码不是UTF8编码,看看函数里是否支持转码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-18 21:49:23
能向你学习一下社会网络嘛?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-19 16:00:21
xuezhongcao 发表于 2017-12-18 21:49
能向你学习一下社会网络嘛?
我也只是初学。。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群