Caroline K
Caroline K

Reputation: 1

R query: adding a header row to .txt files

I have 7 datasets (53 variables each with varying numbers of rows, all > 100k) in .txt format. There is no header row but I know the field names and correct formats for each variable. I have tried using rbind:

df <- read.csv("filepath_to_textfile.txt", header = FALSE, 
                as.is = TRUE,  stringsAsFactors = FALSE)

new_df <- rbind(c(hadm_id, subject_id,... obsinl24), df) 

I get the following error message:

Error in rbind(c(hadm_id, subject_id, .... : object 'hadm_id' not found

I have also tried creating a dataframe:

fieldnames <- data.frame(hadm_id=integer(),
                           subject_id=integer(), ...
                           obsinl24)

but get the following error:

Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame

Upvotes: 0

Views: 1445

Answers (1)

Caroline K
Caroline K

Reputation: 1

The following code did just what I needed:

df <- read.csv("filepath_to_textfile.txt", header = FALSE, as.is = TRUE, stringsAsFactors = FALSE)

names(df) <- c("hadm_id", "subject_id",... "obsinl24")

Upvotes: 0

Related Questions