aruna aib
aruna aib

Reputation: 1

Extract column names as a variable

Is there a way to get row information as a character like shown below

as is below

iris[2,]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
2          4.9           3          1.4         0.2  setosa

Expected output

iris[2,]
"Sepal.Length = 2 , Sepal.Width = 4.9, Petal.Length = 1.4, Petal.Width = 0.2, Species = 'setosa'"

Upvotes: 0

Views: 47

Answers (1)

user2974951
user2974951

Reputation: 10385

> paste0(colnames(iris),"=",iris[2,],collapse=", ")
[1] "Sepal.Length=4.9, Sepal.Width=3, Petal.Length=1.4, Petal.Width=0.2, Species=1"

Edit: if you really want it in your specific format, you will need to modify the data a little bit, that is all the factors need to be converted to strings and padded with a single quote.

> iris$Species=paste0("'",as.character(iris$Species),"'")
> paste0(colnames(iris),"=",iris[2,],collapse=", ")
[1] "Sepal.Length=4.9, Sepal.Width=3, Petal.Length=1.4, Petal.Width=0.2, Species='setosa'"

Upvotes: 3

Related Questions