0

I would like to import a txt file only contains one line as table column name and append with the other csv file with only data. The simplified files are as below:

txt file

Precentral Frontal_Sup Frontal_Sup_Orb

and the csv file

0.9938  0.96507 1.0043

And final outcome I want is

Precentral Frontal_Sup Frontal_Sup_Orb
0.9938       0.96507    1.0043

I've tried this but it does work

AAL <- rbind(read.table('raw data/header.txt', header = TRUE), read.csv('raw data/data.csv', header = FALSE))

And the outcome is

V1         V2       V3
0.9938   0.96507  1.0043

How to solve this? Thanks very much!

4
  • I think you need nm1 <- scan('raw data/header.txt', what ="", sep = " ", quiet = TRUE) and then you name the data read with names(df1) <- nm1 Commented Jun 13, 2019 at 5:08
  • ....and combine that with read.csv("file.csv", col.names=scan(...)) Commented Jun 13, 2019 at 5:09
  • Thanks very much! This works prefectly!! Commented Jun 13, 2019 at 5:20
  • You are rbinding two data frames with different headers. If you had used bind_rows from dplyr you would have noticed that you get 6 columns instead of 3. Commented Jun 13, 2019 at 5:21

1 Answer 1

4

Thanks to akrun and thelatemail. It works by using

AAL <- read.csv("data.csv", col.names=scan('raw data/header.txt', what ="", sep = "\t", quiet = TRUE)) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.