[1] 1 6 6 6 1 6 5 1 5 1 1 6 4 5 5 3 6 3 4 4 3 4 3 1 5 5 6 2 4 3 6 6 2 2 3 2 2 3 6 1 5 2 4 2 6 6 2 1
[49] 2 2 4 6 2 6 5 5 4 6 3 2 1 2 3 4 1 6 5 3 1 4 5 5 5 4 3 1 3 3 6 6 5 2 4 6 6 6 4 2 5 6 4 5 1 1 6 6
[97] 5 6 2 1 1 5 5 1 2 4 3 6 4 5 2 4 1 1 2 3 4 1 1 2 2 2 2 1 3 2 3 5 4 1 1 2 3 2 3 1 2 5 4 4 2 2 6 3
[145] 2 6 1 4 3 5 6 1 5 3 2 3 5 2 4 4 5 5 6 6 4 4 5 2 3 4 1 4 2 1 1 1 5 4 1 3 6 2 6 5 5 1 4 2 4 2 2 1
[193] 1 4 5 2 1 1 3 5
Above is a vector, whose dimension is 200. How to turn this into a vector x
in R? It's tedious if I change it manually.
Another variable is:
[1] "L" "L" "L" "L" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[25] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "L" "L" "L" "F"
[49] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[73] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[97] "F" "L" "L" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[121] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[145] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[169] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[193] "F" "F" "F" "F" "F" "F" "F" "F"
Paste it into Lines
as shown and then use gsub
to remove the [...] portions and finally scan
it in.
Lines <- "
[1] 1 6 6 6 1 6 5 1 5 1 1 6 4 5 5 3 6 3 4 4 3 4 3 1 5 5 6 2 4 3 6 6 2 2 3 2 2 3 6 1 5 2 4 2 6 6 2 1
[49] 2 2 4 6 2 6 5 5 4 6 3 2 1 2 3 4 1 6 5 3 1 4 5 5 5 4 3 1 3 3 6 6 5 2 4 6 6 6 4 2 5 6 4 5 1 1 6 6
[97] 5 6 2 1 1 5 5 1 2 4 3 6 4 5 2 4 1 1 2 3 4 1 1 2 2 2 2 1 3 2 3 5 4 1 1 2 3 2 3 1 2 5 4 4 2 2 6 3
[145] 2 6 1 4 3 5 6 1 5 3 2 3 5 2 4 4 5 5 6 6 4 4 5 2 3 4 1 4 2 1 1 1 5 4 1 3 6 2 6 5 5 1 4 2 4 2 2 1
[193] 1 4 5 2 1 1 3 5
"
v <- scan(text = gsub("\\[\\d+\\]", "", Lines), quiet = TRUE)
length(v)
## [1] 200
Another approach using the same input is to scan
it in as character strings and then use grep
to remove the [...] parts and convert what remains to numeric.
sc <- scan(text = Lines, what = "", quiet = TRUE)
v <- as.numeric(grep("[", sc, fixed = TRUE, invert = TRUE, value = TRUE))
length(v)
## [1] 200
Here is the output from dput(v)
v <-
c(1, 6, 6, 6, 1, 6, 5, 1, 5, 1, 1, 6, 4, 5, 5, 3, 6, 3, 4, 4,
3, 4, 3, 1, 5, 5, 6, 2, 4, 3, 6, 6, 2, 2, 3, 2, 2, 3, 6, 1, 5,
2, 4, 2, 6, 6, 2, 1, 2, 2, 4, 6, 2, 6, 5, 5, 4, 6, 3, 2, 1, 2,
3, 4, 1, 6, 5, 3, 1, 4, 5, 5, 5, 4, 3, 1, 3, 3, 6, 6, 5, 2, 4,
6, 6, 6, 4, 2, 5, 6, 4, 5, 1, 1, 6, 6, 5, 6, 2, 1, 1, 5, 5, 1,
2, 4, 3, 6, 4, 5, 2, 4, 1, 1, 2, 3, 4, 1, 1, 2, 2, 2, 2, 1, 3,
2, 3, 5, 4, 1, 1, 2, 3, 2, 3, 1, 2, 5, 4, 4, 2, 2, 6, 3, 2, 6,
1, 4, 3, 5, 6, 1, 5, 3, 2, 3, 5, 2, 4, 4, 5, 5, 6, 6, 4, 4, 5,
2, 3, 4, 1, 4, 2, 1, 1, 1, 5, 4, 1, 3, 6, 2, 6, 5, 5, 1, 4, 2,
4, 2, 2, 1, 1, 4, 5, 2, 1, 1, 3, 5)
Lines2 <- '
[1] "L" "L" "L" "L" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[25] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "L" "L" "L" "F"
[49] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[73] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[97] "F" "L" "L" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[121] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[145] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[169] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[193] "F" "F" "F" "F" "F" "F" "F" "F"
'
sc <- scan(text = Lines2, what = "", quiet = TRUE)
v2 <- grep("[", sc, fixed = TRUE, invert = TRUE, value = TRUE)
Here is the output from dput(v2)
v2 <-
c("L", "L", "L", "L", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "L", "L", "L", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "L", "L", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F")