'Reading data between markers in R
What is the best way to read a CSV file into R when there are 3 data markers? To obtain the indexes for the start and end markers then get the data between these markers only. Save the new data in separate data frames and then name each variable using the data marker?
Human activity data sample
576 -2404 6556 12512 30.74 3788 -2249
577 -1428 10020 13024 30.69 2308 -1852
..
..
Sample aX aY az Temp gX gY gz
554 375 2648 7412 13612 30.18 -286 -288
..
..
Sample aX aY az Temp gX gY gz
1 -1844 10224 11768 30.69 -332 -387 -62
2 -1876 10192 11708 30.65 -297 -435 -21
3 -1804 10332 11692 30.74 -355 -265 -78
..
..
Sample aX aY az Temp gX gY gz
1068 375 2648 7412 13612 30.18 -286 -288
1069 376 276
..
..
How to achieve any guidance/direction or kindly refer be to any material which will be helpful/beneficial or help me out to complete the task.
Solution 1:[1]
Given the following csv
#read in data as is
data_raw <- readLines("./test.csv")
# find separation lines:
from <- which(grepl("^Sample", data_raw))
to <- c(from[2:length(from)] - 1, length(data_raw))
# create cuncks to read
L <- lapply(seq.int(length(from)),
function(i) paste0(data_raw[from[i]:to[i]],
collapse = "\n"))
# read
lapply(L, data.table::fread)
# [[1]]
# Sample aX aY az Temp gX gY gz
# 1: 554 375 2648 7412 13612 30.18 -286 -288
#
# [[2]]
# Sample aX aY az Temp gX gY gz
# 1: 1 -1844 10224 11768 30.69 -332 -387 -62
# 2: 2 -1876 10192 11708 30.65 -297 -435 -21
# 3: 3 -1804 10332 11692 30.74 -355 -265 -78
#
# [[3]]
# Sample aX aY az Temp gX gY gz
# 1: 1068 375 2648 7412 13612 30.18 -286 -288
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wimpel |

