我有一个字符向量低于值:
[1] tBodyAcc-mean()-X tBodyAcc-mean()-Y tBodyAcc-mean()-Z tBodyAcc-std()-X
[5] tBodyAcc-std()-Y tBodyAcc-std()-Z tGravityAcc-mean()-X tGravityAcc-mean()-Y
[9] tGravityAcc-mean()-Z tGravityAcc-std()-X tGravityAcc-std()-Y tGravityAcc-std()-Z
[13] tBodyAccJerk-mean()-X tBodyAccJerk-mean()-Y tBodyAccJerk-mean()-Z tBodyAccJerk-std()-X
[17] tBodyAccJerk-std()-Y tBodyAccJerk-std()-Z tBodyGyro-mean()-X tBodyGyro-mean()-Y
[21] tBodyGyro-mean()-Z tBodyGyro-std()-X tBodyGyro-std()-Y tBodyGyro-std()-Z
[25] tBodyGyroJerk-mean()-X tBodyGyroJerk-mean()-Y tBodyGyroJerk-mean()-Z tBodyGyroJerk-std()-X
[29] tBodyGyroJerk-std()-Y tBodyGyroJerk-std()-Z tBodyAccMag-mean() tBodyAccMag-std()
[33] tGravityAccMag-mean() tGravityAccMag-std() tBodyAccJerkMag-mean() tBodyAccJerkMag-std()
[37] tBodyGyroMag-mean() tBodyGyroMag-std() tBodyGyroJerkMag-mean() tBodyGyroJerkMag-std()
[41] fBodyAcc-mean()-X fBodyAcc-mean()-Y fBodyAcc-mean()-Z fBodyAcc-std()-X
[45] fBodyAcc-std()-Y fBodyAcc-std()-Z fBodyAccJerk-mean()-X fBodyAccJerk-mean()-Y
[49] fBodyAccJerk-mean()-Z fBodyAccJerk-std()-X fBodyAccJerk-std()-Y fBodyAccJerk-std()-Z
[53] fBodyGyro-mean()-X fBodyGyro-mean()-Y fBodyGyro-mean()-Z fBodyGyro-std()-X
[57] fBodyGyro-std()-Y fBodyGyro-std()-Z fBodyAccMag-mean() fBodyAccMag-std()
[61] fBodyBodyAccJerkMag-mean() fBodyBodyAccJerkMag-std() fBodyBodyGyroMag-mean() fBodyBodyGyroMag-std()
[65] fBodyBodyGyroJerkMag-mean() fBodyBodyGyroJerkMag-std()
我想替换repl模式的名称模式:
l_name_patterns <- c("^t{1}","^f{1}","*BodyBody*","*Body*","*Acc*", "*Gravity*","*Gyro*", "*Mag*", "*Jerk*")
l_repl_patterns <- c("Time ", "Frequency ","Body","Body ","Accelerator ", "Gravity ", "Gyroscope ","Magnitude ", "Jerk ")
我想使用sub或其他类似函数进行多次替换,因为子文档说:
pattern: If a character vector of length 2 or more is supplied, the first element is used with a warning.
我这样做了:
for ( i in 1:length(l_name_patterns) )
{
print(l_name_patterns[i])
l_names <- sub( pattern = l_name_patterns[i]
, replacement = l_repl_patterns[i]
, x = l_names
)
}
任何人都有更优雅的方法吗?
由于
答案 0 :(得分:3)
你可以尝试
library(qdap)
mgsub(l_pattern, l_replacement, l_names)
#[1] "yM tsop on StackOverflow"
对于帖子中的示例
res <- mgsub(l_name_patterns, l_repl_patterns, l_names, fixed=FALSE)
head(res,3)
#[1] "Time Body Accelerator -mean()-X" "Time Body Accelerator -mean()-Y"
#[3] "Time Body Accelerator -mean()-Z"
l_names <- "My post on StackOverflow"
l_pattern <- c("post","My")
l_replacement <- c( "tsop", "yM")
帖子中的示例数据
l_names <- c("tBodyAcc-mean()-X", "tBodyAcc-mean()-Y", "tBodyAcc-mean()-Z",
"tBodyAcc-std()-X", "tBodyAcc-std()-Y", "tBodyAcc-std()-Z", "tGravityAcc-mean()-X",
"tGravityAcc-mean()-Y", "tGravityAcc-mean()-Z", "tGravityAcc-std()-X",
"tGravityAcc-std()-Y", "tGravityAcc-std()-Z", "tBodyAccJerk-mean()-X",
"tBodyAccJerk-mean()-Y", "tBodyAccJerk-mean()-Z", "tBodyAccJerk-std()-X",
"tBodyAccJerk-std()-Y", "tBodyAccJerk-std()-Z", "tBodyGyro-mean()-X",
"tBodyGyro-mean()-Y", "tBodyGyro-mean()-Z", "tBodyGyro-std()-X",
"tBodyGyro-std()-Y", "tBodyGyro-std()-Z", "tBodyGyroJerk-mean()-X",
"tBodyGyroJerk-mean()-Y", "tBodyGyroJerk-mean()-Z", "tBodyGyroJerk-std()-X",
"tBodyGyroJerk-std()-Y", "tBodyGyroJerk-std()-Z", "tBodyAccMag-mean()",
"tBodyAccMag-std()", "tGravityAccMag-mean()", "tGravityAccMag-std()",
"tBodyAccJerkMag-mean()", "tBodyAccJerkMag-std()", "tBodyGyroMag-mean()",
"tBodyGyroMag-std()", "tBodyGyroJerkMag-mean()", "tBodyGyroJerkMag-std()",
"fBodyAcc-mean()-X", "fBodyAcc-mean()-Y", "fBodyAcc-mean()-Z",
"fBodyAcc-std()-X", "fBodyAcc-std()-Y", "fBodyAcc-std()-Z", "fBodyAccJerk-mean()-X",
"fBodyAccJerk-mean()-Y", "fBodyAccJerk-mean()-Z", "fBodyAccJerk-std()-X",
"fBodyAccJerk-std()-Y", "fBodyAccJerk-std()-Z", "fBodyGyro-mean()-X",
"fBodyGyro-mean()-Y", "fBodyGyro-mean()-Z", "fBodyGyro-std()-X",
"fBodyGyro-std()-Y", "fBodyGyro-std()-Z", "fBodyAccMag-mean()",
"fBodyAccMag-std()", "fBodyBodyAccJerkMag-mean()", "fBodyBodyAccJerkMag-std()",
"fBodyBodyGyroMag-mean()", "fBodyBodyGyroMag-std()", "fBodyBodyGyroJerkMag-mean()",
"fBodyBodyGyroJerkMag-std()")
l_name_patterns <- c("^t{1}","^f{1}","BodyBody","Body","Acc",
"Gravity","Gyro", "Mag", "Jerk")
l_repl_patterns <- c("Time ", "Frequency ","Body","Body ","Accelerator ",
"Gravity ", "Gyroscope ","Magnitude ", "Jerk ")
答案 1 :(得分:0)
一种方法是迭代列表,每次转换l_names
:
for (i in seq_along(l_name_patterns)) {
l_names <- sub(l_name_patterns[i], l_repl_patterns[i], l_names)
}