i have large dataset has pattern similar datapattern below. need code create desiredresult dataset
library(data.table) v1 <- rep(c(rep("a", times = 2), letters[2:5], rep("f", times = 2)), times = 2) v2 <- c(c(c(0.24, 0.25), 2:5, c(0.95, 1.05)), c(c(0.34, 0.35), 2:5, c(1.95, 2.05)) ) (datapattern <- data.table(v1, v2)) (desiredresult <- data.table(v1, v2, c(rep(c(0.24, 0.25), times = 4), rep(c(0.34, 0.35), times = 4)), c(rep(c(0.95, 1.05), times = 4), rep(c(1.95, 2.05), times = 4)))) i need create column v3 in desiredresult. pattern follows:
if v1 == "a" v3 = v2 if v1 != "a" repeat previous corresponding set of v2 values until new value of a reached new values of v2 placed in v3, etc. above repeats new values of a.
i need code create column v4 in desiredresult similar column v3 except checks if v1 == "f" , places values of f v2 v4 , repeats if v1 != "f"
i have tried:
rle(datapattern$v1 == "a" ) # run length encoding # lengths: int [1:4] 2 6 2 6 # values : logi [1:4] true false true false the sequence v1 != "a" or v1 != "f" appears equal number of false minus number of true. how many times each a sequence need repeated until new a reached
many thanks
ok, here better way, think, values of v2 column depending on v1=='a'.
v1 <- rep(c(rep("a", times = 2), letters[2:5], rep("f", times = 2)), times = 2) v2 <- c(c(c(0.24, 0.25), 2:5, c(0.95, 1.05)), c(c(0.34, 0.35), 2:5, c(1.95, 2.05)) ) datapattern <- data.frame(v1, v2) datapattern$v3 <- ifelse(datapattern$v1 == "a", datapattern$v2, na) datapattern$v4 <- ifelse(datapattern$v1 == "f", datapattern$v2, na) (i in 1:nrow(datapattern)){ if (datapattern$v1[i] == "a"){ tmpa <- datapattern$v3[i] } if (is.na(datapattern$v3[i])){ datapattern$v3[i] <- tmpa } if (datapattern$v1[nrow(datapattern)-(i-1)] == "f"){ tmpf <- datapattern$v4[nrow(datapattern)-(i-1)] } if (is.na(datapattern$v4[nrow(datapattern)-(i-1)])){ datapattern$v4[nrow(datapattern)-(i-1)] <- tmpf } } output, think more correct, according stated rules, desiredoutput:
> datapattern v1 v2 v3 v4 1 0.24 0.24 0.95 2 0.25 0.25 0.95 3 b 2.00 0.25 0.95 4 c 3.00 0.25 0.95 5 d 4.00 0.25 0.95 6 e 5.00 0.25 0.95 7 f 0.95 0.25 0.95 8 f 1.05 0.25 1.05 9 0.34 0.34 1.95 10 0.35 0.35 1.95 11 b 2.00 0.35 1.95 12 c 3.00 0.35 1.95 13 d 4.00 0.35 1.95 14 e 5.00 0.35 1.95 15 f 1.95 0.35 1.95 16 f 2.05 0.35 2.05
Comments
Post a Comment