我想知道是否可以替换字符并删除一行中的空格。
string = c("av13 personal care", "-11", "av13 personal care", "av14 personal services",
"av15 meals", "av29 visit friends", "av17 free time travel",
"av27 pubs", "av28 restaurants", "av28 restaurants", "av29 visit friends",
"av37 conversation", "av14 personal services", "av13 personal care",
"av13 personal care", "av13 personal care", "-11", "av13 personal care",
"av13 personal care", "av15 meals", "av6 cook, wash up", "av40 other leisure",
"av37 conversation", "av21 walking", "av40 other leisure", "av15 meals",
"av6 cook, wash up", "av13 personal care", "av21 walking", "av17 free time travel",
"av15 meals", "av35 read papers, magazines", "av27 pubs", "av13 personal care",
"-11", "av13 personal care", "av2 paidwork at home", "av25 dances or parties",
"av1 paid work", "av1 paid work", "av1 paid work", "av1 paid work",
"av2 paidwork at home", "av2 paidwork at home", "av13 personal care",
"av17 free time travel", "av29 visit friends", "av17 free time travel",
"av13 personal care", "-11", "av13 personal care")
而不是总是这样做
clean = gsub(pattern = "[A-z]", replacement = "", x = string)
clean = gsub(pattern = "[[:blank:]]", replacement = "", x = clean)
是否可以直接在第一行插入[[:blank:]]
?
我也遇到","
("35,"
)的问题,我怎样才能在第一行摆脱它?
小更新我意识到在我的(巨大的)数据集中,/
我的gsub
之后我也会得到string
- 您是否也可以帮我删除它?
答案 0 :(得分:4)
这是一个单行:
gsub(pattern = "[A-z ,/]", replacement = "", x = string) # added / to address the update
或
gsub(pattern = "[A-z]| |,|/", replacement = "", x = string)
正如hwnd所指出的,范围[A-z]
实际上包含了几个特殊字符,它们位于Ascii代码(relevant SO answer和ASCII table)中的A-Z和a-z之间。这些特殊字符包括:[
,\
,]
,^
,_
和`
答案 1 :(得分:3)
字符类connect
也会匹配其他字符,我会使用:
void MyClass::handleCheckedChanged(QStandardItem *item) {
const QModelIndex index = item->model()->indexFromItem(item);
QItemSelectionModel *selModel = poListView->selectionModel();
selModel->select(QItemSelection(index, index), item->checkState() == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
}
void MyClass::handleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
foreach (const QModelIndex &index, selected.indexes()) {
index.model()->itemFromIndex(index)->setCheckState(Qt::Checked);
}
foreach (const QModelIndex &index, deselected.indexes()) {
index.model()->itemFromIndex(index)->setCheckState(Qt::Unchecked);
}
}
...
connect(poModel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(handleCheckedChanged(QStandardItem *)));
connect(poListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(handleSelectionChanged(QItemSelection, QItemSelection)));
注意: POSIX括号中的表达式[A-z]
与空格和制表符匹配。
如果只需要空间,那么:
gsub('[a-zA-Z\t ,]', '', string)