I want to rename columns in one dataset based on the values from another dataset, for example...
> set.seed(1234)
> questions = data.frame(Area=c("Zone1","Zone2","Zone3"),
X1a=sample(10,3), X1b=sample(10,3), X1c=sample(10,3),
X1d=sample(10,3), X1e=sample(10,3))
>questions
Area X1a X1b X1c X1d X1e
1 Zone1 2 7 1 6 3
2 Zone2 6 8 3 7 9
3 Zone3 5 6 6 5 10
answers = data.frame(F1=c("question1","question2","question3","question4","question5"))
> answers
F1
1 question1
2 question2
3 question3
4 question4
5 question5
Now I want to replace X1a,X1b,etc... with the contents in answers, So I tried using names()
> names(questions)[2:6]<-c(answers[1,],answers[2,],answers[3,],answers[4,],answers[5,])
But the result I get is...
Area 1 2 3 4 5
1 Zone1 2 7 1 6 3
2 Zone2 6 8 3 7 9
3 Zone3 5 6 6 5 10
I seem to get a rownumber instead of the actual contents of the cells, I also get the same result from using...
names(questions)[2:6]<-c(answers[1,1],answers[2,1],answers[3,1],answers[4,1],answers[5,1])
Is there something simple I´m overlooking here?
What about: names(questions) <- c("Area",t(answers))
You need to convert from factor to string.
(Strangly my in between version: names(questions) <- t(answers$F1)
worked)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.