我试图添加"错误"使用extract()在我的测量值旁边的列。但是,我想我已经开始使用正则表达式和/或extract()语法了。希望得到一些帮助。
理想情况下,我应该使用列
获得长格式Reading Category Measurement Error Sample
可重现的代码
Reading <- c(1,2,3,4)
Cat1 <- runif(4)*10
Cat1_err <- runif(4)/10
Cat2 <- runif(4)*10
Cat2_err <- runif(4)/10
Cat3 <- runif(4)*10
Cat3_err <- runif(4)/10
Sample <- c("X14","X23","X11","X10")
df_wide <- data.frame(Reading,Cat1,Cat1_err,Cat2,Cat2_err,Cat3,Cat3_err,Sample)
df_wide
Reading Cat1 Cat1_err Cat2 Cat2_err Cat3 Cat3_err Sample
1 1 7.375116 0.01014747 2.234376 0.08978868 5.373709 0.02245759 X14
2 2 5.097937 0.07036843 5.691806 0.05561866 1.823026 0.07658357 X23
3 3 2.034116 0.01689391 8.192971 0.03844054 4.242167 0.01036751 X11
4 4 9.129536 0.09130868 5.908125 0.05505775 5.747843 0.05774527 X10
df_long <- df_wide %>%
+ gather(key=Category, value=Measurement, Cat1:Cat3_err, factor_key = TRUE) %>%
+ extract(Measurement,c("Meas","Error"),"Cat\d_err", remove=FALSE)
Error in names(l) <- enc2utf8(into) :
'names' attribute [2] must be the same length as the vector [0]
答案 0 :(得分:1)
我认为您不想使用simplified = modifer.modify(geometry, geometry.vertices.length * 0.5 | 0);
simplified.computeFaceNormals();
。我认为extract
和separate
可能就是你想要的。以下内容将生成警告消息,但有效。
spread
答案 1 :(得分:1)
可能有一种更快的方法可以做到这一点,但它看起来像你正在寻找的那样:
df_wide %>%
gather(key=Category, value=Measurement, Cat1:Cat3_err, factor_key = TRUE) %>%
extract(Category,c("Meas","Error"),"(Cat\\d)[_]*([a-z]*)") %>%
spread(key = Error, value = Measurement)
请注意,除其他事项外,需要在R中使用\\d
表示正则表达式。