Insert variable names in the first line of the dataframe

Asked

Viewed 373 times

-1

I want to insert the name of the variables as the first line of my datafrane. For example:

   X11.3116555055603 X16.8234904296696
1           14.79609          10.37313
2           10.02141          16.38942
3           12.10137          18.75196
4           14.29997          15.90409
5           15.07731          13.77788
6           19.33323          19.92858
7           13.41967          17.74106
8           11.96044          19.09498
9           19.13904          16.15948

but I want to:

1           X11.3117          X16.8235
2           14.79609          10.37313
3           10.02141          16.38942
4           12.10137          18.75196
5           14.29997          15.90409
6           15.07731          13.77788
7           19.33323          19.92858
8           13.41967          17.74106
9           11.96044          19.09498
10          19.13904          16.15948

dput:

data=structure(list(X11.3116555055603 = c(14.7960854065605, 
10.0214068288915, 
                                      12.1013720962219, 14.299969333224, 
15.077308237087, 19.3332333257422, 
                                      13.4196697315201, 11.9604384386912, 
19.1390363522805), X16.8234904296696 = c(10.3731271764264, 

16.3894248008728, 18.7519607762806, 15.9040869213641, 13.7778785359114, 

19.9285765457898, 17.7410648972727, 19.0949787106365, 16.1594827705994
                                      )), class = "data.frame", row.names = 
2:10)
  • Are you aware that in doing so, the values of line 2 onwards become characters? And in this way, it will be impossible to use these values to calculate mean, standard deviation, median or any other statistic that does not involve counting or proportions?

  • Yes, I know that, @Marcusnunes. But then I perform the necessary conversions.

1 answer

1


There is probably a more practical way but it follows code that returns the structure you expect:

data[nrow(data)+1,] <- names(data)
data <- data[c(nrow(data), 1:(nrow(data)-1)),]
rownames(data) <- 1:nrow(data)
names(data) <- NULL

First I add the column name as the last row of data.frame ; then I reorder the data so that the name is the first row; I modify the row name and finally, "delete" the column name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.