[英]create r vector with names
我可以使用靜態輸入創建以下輸出:
t1 = c("dog"="dog","cat"= "cat")
t1
Results:
dog cat
"dog" "cat"
我如何只用下面的t $動物字符向量創建相同的結果
t = data.frame(animal = c("dog","cat"))
c(t$animal =t$animal) # this does not work
轉換為字符,然后使用names<-
(或setNames
):
ch <- as.character(unlist(t))
names(ch) <- ch
ch
## dog cat
## "dog" "cat"
您可以使用setNames
t2 <- setNames(t$animal, t$animal)
t2
#dog cat
#dog cat
數據
t <- data.frame(animal = c("dog","cat"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.