w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Use function to convert messy data in many data frames to single tidier format

I think you over complicate the task:

  1. Use read.table with skip option to skip first lines
  2. Convert extract x columns as a single vector : volume
  3. Convert extract y columns as a single vector : flow
  4. Use recycling to create other columns

here my code:

## here you change text by your file=file_name
d_f <- read.table(text=dat,header=TRUE,skip=3)[,-1]
## extract only X column and expand them in a single vector
volume <- unlist(as.list(d_f[grep('X',colnames(d_f))]))
## extract only Y column and expand them in a single vector
flow <- unlist(as.list(d_f[grep('Y',colnames(d_f))]))
## create you data frame using recycling for other columns
data.frame(id=1,name='Ball',routine='tech',
       trial='pos1',volume=volume,flow=flow)

   id name routine trial     volume   flow
X1    1 Ball    tech  pos1 0.00000000 0.0000
X2    1 Ball    tech  pos1 0.05022222 2.1945
X3    1 Ball    tech  pos1 0.10044444 5.7684
X.11  1 Ball    tech  pos1 0.00000000 0.0000
X.12  1 Ball    tech  pos1 0.05022222 1.9437
X.13  1 Ball    tech  pos1 0.10044444 4.7652
X.21  1 Ball    tech  pos1 0.00000000 0.0000
X.22  1 Ball    tech  pos1 0.05000000 1.2540
X.23  1 Ball    tech  pos1 0.10000000 4.2636




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.