I have a dataframe but wish to use a column as header
The current columns are labelled 1,2,3,4,5
The first row has actual columns I want <1, 1-4, 5-9, 10-14
How can I move the first row to become the header?
I have a dataframe but wish to use a column as header
The current columns are labelled 1,2,3,4,5
The first row has actual columns I want <1, 1-4, 5-9, 10-14
How can I move the first row to become the header?
if your data is called mydata
then run dput(head(mydata))
and share with me here, and I will try to help you
structure(list(...2 = c("<1", "1-4", "5-9", "10-14", "15-19",
"20-24"), ...3 = c(48, 8, 4, 4, 6, 11), ...4 = c(50, 9, 8, 9,
16, 23), ...5 = c(69, 7, 5, 4, 10, 25), ...6 = c(53, 9, 4, 8,
15, 30), ...7 = c(50, 6, 5, 4, 23, 23), ...8 = c(30, 8, 4, 4,
10, 34), ...9 = c(43, 6, 2, 4, 16, 26), ...10 = c(51, 5, 6, 7,
20, 18), ...11 = c(49, 7, 6, 7, 24, 25), ...12 = c(56, 11, 2,
7, 21, 23), ...13 = c(53, 13, 3, 6, 18, 39), ...14 = c(44, 2,
6, 4, 15, 22), ...15 = c(49, 8, 1, 4, 12, 17), ...16 = c(51,
8, 5, 8, 9, 20), ...17 = c(38, 6, 4, 4, 8, 16)), row.names = 21:26, class = "data.frame")
structure(list(...2 = c(
"<1", "1-4", "5-9", "10-14", "15-19",
"20-24"
), ...3 = c(48, 8, 4, 4, 6, 11), ...4 = c(
50, 9, 8, 9,
16, 23
), ...5 = c(69, 7, 5, 4, 10, 25), ...6 = c(
53, 9, 4, 8,
15, 30
), ...7 = c(50, 6, 5, 4, 23, 23), ...8 = c(
30, 8, 4, 4,
10, 34
), ...9 = c(43, 6, 2, 4, 16, 26), ...10 = c(
51, 5, 6, 7,
20, 18
), ...11 = c(49, 7, 6, 7, 24, 25), ...12 = c(
56, 11, 2,
7, 21, 23
), ...13 = c(53, 13, 3, 6, 18, 39), ...14 = c(
44, 2,
6, 4, 15, 22
), ...15 = c(49, 8, 1, 4, 12, 17), ...16 = c(
51,
8, 5, 8, 9, 20
), ...17 = c(38, 6, 4, 4, 8, 16)), row.names = 21:26, class = "data.frame") -> problem_frame
almost_there <- t(problem_frame) %>% as_data_frame()
names(almost_there) <-problem_frame[,1]
closer <- almost_there %>% slice(-1)
finished <- closer %>% mutate_all(as.integer)
Thanks for this but it's a bit complicated for my level right now, I just want a simple way of making Row 2 become the header?
That's it.
Just use it using the name of your df in place of problem_frame.
Or arrange for the data to have the required shape in excel first before exporting it, and use a function like readr::read_csv() to load it in
Thanks for your help, I really really appreciate you took time out of your day to help me.
But I want to be able to do this in R as I have large datasets.
I don't understand the code you sent me back, can you do a simpler explanation?
every function I use you can get help on, like if you wanted to read about t() you type ?t into the console
almost_there <- t(problem_frame) %>% as_data_frame()
I transpose the problem_frame, which means rows become columns and columns become rows. the result is a matrix though, so make it a dataframe
names(almost_there) <-problem_frame[,1]
the first column holds the names. if you type problem_frame[,1]
you see its the first column.
I want these to be the column names of almost_there. the names() function does that.
closer <- almost_there %>% slice(-1)
I throw away the first row as it duplicates the column headings
Then the last step is possible most complicated and related to your knowledge of the data. your example is entirely integer so i change every column to contain integers (and not characters)
finished <- closer %>% mutate_all(as.integer)
this approach relies on library(tidyverse) which you should install if you havent
This was absolutely perfect!!!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.