R,数据解析器到df

I have 1 col input.csv, I want to covert to three col, base on some logic cal. I have done output.csv by excel formula, can work, but now I want to run in R... So far, in R, my thinking is to assign some category in col 2, as draft.R below. Pls kindly advice how to improve it. Pls kindly see google excel link for details.

谢谢

#draft.R
in1 = read.csv("input.csv")
df2 <- in1
df2$V2 <- ifelse(
grepl("LAYER",df2$V1),1,
ifelse(grepl("DATATYPE",df2$V1),2,0
)
)
df2 <- as.data.frame(df2)
df2

#input.csv
V1
LAYER: 34 ;
DATATYPE: 0 ;
0;0
10;0
10;0.9
0;0.9
0;0
LAYER: 34 ;
DATATYPE: 0 ;
0;9.1
10;9.1
10;10
0;10
0;9.1
LAYER: 44 ;
DATATYPE: 10 ;
9.52;1.3
9.8;1.3
9.8;9.1
9.52;9.1
9.52;1.3

#I expect output.csv    
V1  group   L;D
0;0 1   34;0
10;0    1   34;0
10;0.9  1   34;0
0;0.9   1   34;0
0;0 1   34;0
0;9.1   2   34;0
10;9.1  2   34;0
10;10   2   34;0
0;10    2   34;0
0;9.1   2   34;0
9.52;1.3    3   44;0
9.8;1.3 3   44;0
9.8;9.1 3   44;0
9.52;9.1    3   44;0
9.52;1.3    3   44;0
评论