In R Data.Frame, Promote Rownames to Actual Column

In R data.frame, promote rownames to actual column

df <- data.frame(freq=c(4,2,9,4,1),row.names=c('hello','sorry','bad','good','great'));
df;
## freq
## hello 4
## sorry 2
## bad 9
## good 4
## great 1
df <- data.frame(word=rownames(df),freq=df$freq);
df;
## word freq
## 1 hello 4
## 2 sorry 2
## 3 bad 9
## 4 good 4
## 5 great 1

Edit: On second thought, a better way to do this, which avoids having to hard-code the name of the existing column, and will work with any number of existing columns, is as follows:

df <- data.frame(word=rownames(df),df,row.names=NULL);
df;
## word freq
## 1 hello 4
## 2 sorry 2
## 3 bad 9
## 4 good 4
## 5 great 1

Put the first row as the column names of my dataframe with dplyr in R

Try this:

library(dplyr)
library(tidyr)

x <- data.frame(
A = c(letters[1:10]),
M1 = c(11:20),
M2 = c(31:40),
M3 = c(41:50))

x %>%
gather(key = key, value = value, 2:ncol(x)) %>%
spread(key = names(x)[1], value = "value")
key a b c d e f g h i j
1 M1 11 12 13 14 15 16 17 18 19 20
2 M2 31 32 33 34 35 36 37 38 39 40
3 M3 41 42 43 44 45 46 47 48 49 50

How to change the first row to be the header in R?

If you don't want to re-read the data into R (which it seems like you don't from the comments), you can do the following. I had to add some zeros to get your data to read completely, so disregard those.

dat
## V2 V3 V4 V5 V6 V7 V8 V9 V10
## 17 Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status
## 18 74136 0 1 0 918-491-6998 0 918-491-6659 0 1
## 19 30329 1 0 0 404-321-5711 0 0 0 1
## 20 74136 1 0 0 918-523-2516 0 918-523-2522 0 1
## 21 80203 0 1 0 303-864-1919 0 0 0 1
## 22 80120 1 0 0 345-098-8890 456 0 0 1

First take the first row as the column names. Next remove the first row. Finish it off by converting the columns to their appropriate types.

names(dat) <- as.matrix(dat[1, ])
dat <- dat[-1, ]
dat[] <- lapply(dat, function(x) type.convert(as.character(x)))
dat
## Zip CuCurrent PaCurrent PoCurrent Contact Ext Fax email Status
## 1 74136 0 1 0 918-491-6998 0 918-491-6659 0 1
## 2 30329 1 0 0 404-321-5711 0 0 0 1
## 3 74136 1 0 0 918-523-2516 0 918-523-2522 0 1
## 4 80203 0 1 0 303-864-1919 0 0 0 1
## 5 80120 1 0 0 345-098-8890 456 0 0 1

Create a data.frame from multiple textfiles with rownames as columns in r

A possible solution using {purrr}'s map_dfr to map a list of file names to a (custom) function to read the data. The main advantages of such a solution is that you don't have to create a list of data frames
to merge them together afterwards and by avoiding the loop you don't have to create temporary objects that would clutter your working environment. All objects created within the function only live within the function.

The disadvantage is that it might be harder in the beginning to understand what goes on behind the scenes, while writing a for loop, every step is more explicit. If you have the time, I encourage you to take a time to The Joy of Functional Programming (for Data Science) video from Hadley Wickham. At around 8 minutes onwards he talks about exactly this kind of problem you are facing. But the whole video is worth its time! :)

library(tidyverse)
datalist <- list.files("data/newspaper", full.names = T)

custom_read_lines <- function(file) {
# define function to read files and return a data frame already as expected
# in final output
whole_file <- readLines(file)
text <- paste(whole_file[5:length(whole_file)], collapse = '')

df <- data.frame(
Newspaper = whole_file[1],
Date = whole_file[2],
Ressort = whole_file[3],
Title = whole_file[4],
Text = text
)

return(df)
}

## using purrr's map_dfr to map each entry of data list to the custom function
df_merged <- datalist %>% map_dfr(custom_read_lines)

df_merged %>% as_tibble() #just for nicer output
# A tibble: 3 x 5
# Newspaper Date Ressort Title text
# <chr> <chr> <chr> <chr> <chr>
# 1 Nice News 2021-01-… ressort Where does it co… "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of …
# 2 Boring News 2020-11-… ressort Why do we use it? "It is a long established fact that a reader will be distracted by the readable content of a pa…
# 3 Old News 1990-01-… ressort What is Lorem Ip… "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has bee…

# in case your data looks a bit different and you want to try out first you can always try
# the function for a single file, if it works you can then pass it to the map_dfr function

first_file <- datalist[2]
custom_read_lines(first_file) %>% as_tibble()
# A tibble: 1 x 5
# Newspaper Date Ressort Title text
# <chr> <chr> <chr> <chr> <chr>
# 1 Boring News 2020-11-… ressort Why do we us… It is a long established fact that a reader will be distracted by the readable content of a page wh…

each of the 3 examples files looked more or less like the following:

Nice News
2021-01-01
ressort
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text.
It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.

How to put a column as row names in a Dataframe

Maybe you're looking for this:

> DF <- DF[, -1]
> colnames(DF)[1] <- 'rownames'
> DF
rownames Amount1
1 15 15
2 9812 25
3 1672 89

making the first row a header in a dataframe in r

Works for me (with a little trick).

x <- read.table(text = "File Fp1.PD_ShortSOA_FAM Fp1.PD_LongSOA_FAM Fp1.PD_ShortSOA_SEMplus_REAL Fp1.PD_ShortSOA_SEMplus_FICT
sub0001 0,446222 2,524,804 0,272959 1,281,349
sub0002 1,032,688 2,671,048 1,033,278 1,217,817",
header = TRUE)

x <- t(x)
colnames(x) <- x[1, ]
x <- x[-1, ]
x

sub0001 sub0002
Fp1.PD_ShortSOA_FAM "0,446222" "1,032,688"
Fp1.PD_LongSOA_FAM "2,524,804" "2,671,048"
Fp1.PD_ShortSOA_SEMplus_REAL "0,272959" "1,033,278"
Fp1.PD_ShortSOA_SEMplus_FICT "1,281,349" "1,217,817"


Related Topics



Leave a reply



Submit