I want to read in the data set from this website into R: https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt
There is an ID column without a column name in the first column. How can I either not read in the ID column or read in the ID column with a blank/new title?
CodePudding user response:
import all columns (then change name)
# load package
library(data.table)
# import txt
df <- fread(file.choose())
# change 1st column's name
names(df)[1] <- 'id'
import all except 1st column
df <- fread(file.choose(), drop = 1)
CodePudding user response:
We can use setnames to change the first column's name (which is automatically named V1) when you read in the file, and do it all in one step:
library(data.table)
dt <-
setnames(
fread(
"https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt"
),
"V1",
"ID"
)
Output
ID neuroticism extraversion sex volunteer
1: 1 16 13 female no
2: 2 8 14 male no
3: 3 5 16 male no
4: 4 8 20 female no
5: 5 9 19 male no
---
1417: 1417 5 10 male yes
1418: 1418 8 4 female yes
1419: 1419 8 8 male yes
1420: 1420 19 20 female yes
1421: 1421 15 20 male yes
If you want to ignore the first column, then you can either use drop (as suggested in the other answer), or you can also use select to keep the columns that you want.
dt <- fread(
"https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt", select = c(2:5)
)
neuroticism extraversion sex volunteer
1: 16 13 female no
2: 8 14 male no
3: 5 16 male no
4: 8 20 female no
5: 9 19 male no
---
1417: 5 10 male yes
1418: 8 4 female yes
1419: 8 8 male yes
1420: 19 20 female yes
1421: 15 20 male yes
