In my dataset (which is on gun violence), each column has || in between the data points.
e.g Age
0::Male||1::Female||2::Male||
How do you separate the data points?
Thanks!
In my dataset (which is on gun violence), each column has || in between the data points.
e.g Age
0::Male||1::Female||2::Male||
How do you separate the data points?
Thanks!
read.table
/read.delim
allows you define a single character by which values in each line are separated (see argument sep
in ?read.table
).
Since you have ||
, all we need to do is remove the resulting NA
columns when reading the data.
Here is an example:
# Sample data
df <- read.table(text =
"0::Male||1::Female||2::Male||
0::Male||1::Female||2::Male||
0::Male||1::Female||2::Male||
0::Male||1::Female||2::Male||", sep = "|")
# Remove NA columns
df[, !sapply(df, function(x) all(is.na(x)))]
# V1 V3 V5
#1 0::Male 1::Female 2::Male
#2 0::Male 1::Female 2::Male
#3 0::Male 1::Female 2::Male
#4 0::Male 1::Female 2::Male