I am a turbo noob at R but I am looking to get my feet wet more with it. I have a text file with numbers that looks like:
1 2 10 15
(I can also produce these numbers with a comma and space if needed i.e 1, 2, 10, 15
)
I am attempting to read these numbers as a numerical vector so I can work with them in basic calculations such as graphing, mean, etc. I attempted to use:
data <- scan(file="./logs_formatted/Packets_Delivered_Poll_Formatted.log", what="numeric")
However, this produces my numbers as characters like: "1" "2" "10" "15". This is fine but I cant seem to do any arithmetic with these characters. I basically just want to read this file and have a vector like:
c(1, 2, 10, 15)
How do I do this?
I have tried a few functions.
CodePudding user response:
When using scan()
, the what
argument should not be in quotations. It should be double()
, character()
, numeric()
, etc. Since it's not specified correctly, I believe it's defaulting to double()
.
yourvector <- c(1, 2, 10, 15)
write(yourvector, "myfile.txt")
numvector <- scan(file = "myfile.txt", what = numeric(), quote = "")
numvector
#> [1] 1 2 10 15
class(numvector)
#> [1] "numeric"
class(numvector[[1]])
#> [1] "numeric"
CodePudding user response:
We may need numeric()
data <- scan(file="./logs_formatted/Packets_Delivered_Poll_Formatted.log", what=numeric(),
quiet = TRUE)
-testing
scan(text = "1 2 10 15", what = numeric(), quiet = TRUE)
# [1] 1 2 10 15