[英]replace dataset values r by another indexed values
我有以下數據集
head(data)
from to
1 1 2
2 2 3
3 2 17
4 3 4
5 4 5
6 4 855
我有這個數據集
> head(names)
V1
1 Greenock
2 Glasgow
3 Preston
4 Birmingham
5 Southampton
6 Le
現在我想要的很簡單:
head(data)
from to
1 Greenock Glasgow
2 Glasgow Preston
3 Glasgow 17 (you got the point)
4 Preston Birmingham
5 Birmingham Southampton
6 Birmingham 855
我嘗試了這種舊時尚,但
> for(i in 1:nrow(data)){
+ data$from[i] <- names$V1[data$from]
+ data$to[i] <- names$V1[data$to]
+ }
有任何想法嗎?
R的factor
是針對此類數據制定的。 它將數據保留為數字,但增加了人類可讀的level
s。
我只是將from
和to
列轉換為factor
s:
data$from <- factor(data$from)
data$to <- factor(data$to)
然后更改級別的標簽:
levels(data$from) <- names$V1
levels(data$to) <- names$V1
上面的代碼對我有用:
data <- data.frame(
from = 1:10,
to = seq(from=10, to=1, by=-1))
names <- data.frame(
V1 = c('a','b','c','d','e', 'f','g','h','i','j'))
data$from <- factor(data$from)
data$to <- factor(data$to)
levels(data$from) <- names$V1
levels(data$to) <- names$V1
print(data)
結果是:
from to
1 a j
2 b i
3 c h
4 d g
5 e f
6 f e
7 g d
8 h c
9 i b
10 j a
此答案的確假定您為每個數字都有一個標簽。 如果不是這種情況,則通常意味着數據有問題,並且您希望引發錯誤。 您應該使用stopifnot
或(更好)來自Hadley的assertthat包的assert_that
來斷言max(data[,c('to','from')]) <= nrow(names)
(未經測試)。
如果您不想做此假設,則應使用@RichardScriven的答案。
這是使用某些邏輯子集和replace()
的一種方法。
dlg <- data <= nrow(names)
replace(data, dlg, as.character(names$V1)[unlist(data)][dlg])
# from to
# 1 Greenock Glasgow
# 2 Glasgow Preston
# 3 Glasgow 17
# 4 Preston Birmingham
# 5 Birmingham Southampton
# 6 Birmingham 855
順便說一句, data
和names
都是重要基礎函數的名稱,因此您可能需要重命名數據集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.