我有可以使用静态输入创建的输出:
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
答案 0 :(得分:1)
转换为字符,然后使用names<-
(或setNames
):
ch <- as.character(unlist(t))
names(ch) <- ch
ch
## dog cat
## "dog" "cat"
答案 1 :(得分:0)
您可以使用setNames
t2 <- setNames(t$animal, t$animal)
t2
#dog cat
#dog cat
数据
t <- data.frame(animal = c("dog","cat"))