# simulate data
id=c(1:30)
house_type=sample(c("3STR","2STR","1STR","DETC,1STR","2STR,DETC","OTHERS"),20, replace=TRUE)
house=data.table(id,house_type)
# I want to create a house_type2 variable in house data.table
# In the real data, there are many other house type, with 1/2/3STR embeded in these house_typ.
# I want to extract the STR (how many story) information from the data.
house[,story:="others"][str_detect("3STR",house_type),story:="3STR"][
str_detect("2STR",house_type),story:="2STR"][
str_detect("1STR",house_type),story:="1STR"]
模拟数据看起来确实是样本;在实际数据中,house_type字符串的变化更大。我设法用data.table链接语法做到这一点。但是我在问一种更优雅的方法:定义一个函数以提取故事并将该函数应用于house_type列。
答案 0 :(得分:1)
您可以使用正则表达式提取数字:
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $comment->comment_post_ID,
));
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_url($attachment->ID);
}
}
?>
<?php endif;?>
<?php endforeach;?>