说一个数据集:
a <- c(101,101,102,102,103,103)
b <- c("M","M","P","P","M","M")
dt <- as.data.frame(cbind(a,b))
dt
a b
1 101 M
2 101 M
3 102 P
4 102 P
5 103 M
6 103 M
Run Code Online (Sandbox Code Playgroud)
a 列是subject_ID,b 列是subject_name。我想将主题 ID 101 唯一重命名为 M1,将 103 重命名为 M2。
有没有办法通过索引来做到这一点?
这是行不通的。
dt.try1 <- gsub("M","M1",dt[1:2,c(2)])
dt.try1
[1] "M1" "M1"
Run Code Online (Sandbox Code Playgroud)
这是理想的结果:
a b
1 101 M
2 101 M
3 102 P
4 102 P
5 103 M2
6 103 M2
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
我正在尝试做一些应该简单且常规的事情。\n我想搜索文件 /etc/dhcpcd.conf 并更改分配给 eth0 的未注释的静态 IP 地址。示例测试文件如下所示:
\ninterface eth0\n# test variants of comments\n#static ip_address=192.168.21.40/24\n# static ip_address=192.168.21.40/24\n #static ip_address=192.168.21.40/24\n # static ip_address=192.168.21.40/24\n\n#the line to match and change\nstatic ip_address=123.223.122.005/24\nRun Code Online (Sandbox Code Playgroud)\n我尝试在这里实现一个解决方案: \n这里\xe2\x80\x99s 是一种在 awk 中执行此操作的不那么神秘的方法,由 Scott 回答 - \xd0\xa1\xd0\xbb\xd0\xb0\xd0\xb2\ xd0\xb0\xd0\xa3\xd0\xba\xd1\x80\xd0\xb0\xd1\x97\xd0\xbd\xd1\x96
\n我选择 AWK 解决方案是因为它很容易阅读,但是我无法修改答案以适合我。我之前没有使用过sed,awk所以我写了一个测试脚本。
#!/bin/bash\n# testing on a copy of the config file\nconf_file="/home/user/dhcpcd.conf"\n# BASH variable with fake test ip\nnew_ip=111.222.123.234\n\ncat "$conf_file" | awk $\'\n /interface / { matched=0 }\n /interface eth0/ { matched=1 }\n matched …Run Code Online (Sandbox Code Playgroud) 我想从下面的代码中替换所有4个数字300的实例(我的网站用户将在创建新博客文章时粘贴它),使用470.
<div>
<object width="300" height="300">
<embed src="link-removed" width="300" height="300"></embed>
</object>
<p>
<a href="another-link">link</a>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
要粘贴的代码的宽度和高度可能不总是300乘300.
所以我想我可能需要一个正则表达式,它包含字符串"width ="和"height ="之后的任何数值,同时记住要考虑数字周围的引号.任何人都可以告诉我,这是最好的方式,如果是这样,最好的正则表达式是什么?
如果它很重要,被粘贴的代码将作为"文本"存储在数据库中而不是字符串中,因为它非常冗长(我已从您在此处粘贴的内容中删除了几百个字符)...
这让我疯狂的时间远远超过应有的时间,我正在使用SIMPLE字符串替换,但它根据获得的信息无法替换字符串(在这种情况下,它是'url').
class Test
myURL = 'www.google.com'
puts 'Where are you from?'
location = gets
if location == 'England'
myURL['.com'] = '.co.uk'
elsif location == 'France'
myURL['.com'] = '.co.fr'
end
puts myURL
end
Run Code Online (Sandbox Code Playgroud)
我疯了吗?
我有一个由+10万条记录组成的数据框(all_postcodes).[编辑]这里只是几条记录:
pcode area east north area2 area3 area4 area5
AB101AA 10 394251 806376 S92000003 S08000006 S12000033 S13002483
AB101AB 10 394232 806470 S92000003 S08000006 S12000033 S13002483
AB101AF 10 394181 806429 S92000003 S08000006 S12000033 S13002483
AB101AG 10 394251 806376 S92000003 S08000006 S12000033 S13002483
Run Code Online (Sandbox Code Playgroud)
我想使用以下函数创建一个包含其中一列的规范化版本的新列:
pcode_normalize <- function (x) {
x <- gsub(" ", " ", x)
if (length(which(strsplit(x, "")[[1]]==" ")) == 0) {
x <- paste(substr(x, 1, 4), substr(x, 5, 7))
}
x
}
Run Code Online (Sandbox Code Playgroud)
我试着按如下方式执行它:
all_postcodes$npcode <- sapply(all_postcodes$pcode, pcode_normalize)
Run Code Online (Sandbox Code Playgroud)
但这需要太长时间.有什么建议如何提高性能?
什么是*****的正则表达式?(*之间没有空格)
gsub(pattern = "??", replacement="", txt1)
Run Code Online (Sandbox Code Playgroud)
我已经完成了 [**]*$, [^**]+$, [**]*$
我的数据格式为字符向量:
"2014-03-27 11:42:32" "2014-04-03 07:13:28" "0000-00-00 00:00:00" "2012-04-16 12:46:03"
[5] "0000-00-00 00:00:00" "0000-00-00 00:00:00" "2014-04-23 09:33:23" "2014-04-30 06:31:54"[9] "2012-04-18 09:55:44" "2013-11-20 14:43:11"
Run Code Online (Sandbox Code Playgroud)
我想做的是使用一年中的单个数字.2014年的IE sub 4,2013年的3,2012的2,以及0000-00的1 ...除了代表年份的单个数字,我想删除所有其他数字和字符.
我知道我可以使用正则表达式gsub(pattern="2014", replacement="4", logVector)或某些变体来完成我的任务,但我并不精通正则表达式.有人能够提供语法方面的帮助吗?
我的数据如下所示:
duration obs another
1 1.801760 ID: 10 DAY: 6/10/13 S orange
2 1.868500 ID: 10 DAY: 6/10/13 S green
3 0.233562 ID: 10 DAY: 6/10/13 S yellow
4 5.538760 ID:96 DAY: 6/8/13 T yellow
5 3.436700 ID:96 DAY: 6/8/13 T blue
6 0.533856 ID:96 DAY: 6/8/13 T pink
7 2.302250 ID:96 DAY: 6/8/13 T orange
8 2.779420 ID:96 DAY: 6/8/13 T green
Run Code Online (Sandbox Code Playgroud)
我只包含了3个变量,但实际上我的数据有很多.我的问题是看丑陋的"obs"变量.我从另一个人那里收到了这些数据,这些人不一致地将这些信息输入到他们正在使用的软件中.
'obs'包含三条信息: - id(ID:10,ID:96等) - 日期(M/D/Y) - 标识符(S或T)
我想分割这些信息并提取ID号(10或96),日期(例如6/8/13)和标识符(S或T).
为此,我尝试使用strsplit进行以下操作:
temp<-strsplit(as.character(df$obs), " ")
mat<-matrix(unlist(temp), ncol=5, byrow=TRUE)
Run Code Online (Sandbox Code Playgroud)
我认为这可以像我的实际数据那样工作,我有130,000个观察结果,我没有意识到某些观察结果存在id在"ID:"和数字之间没有空格的问题.例如,在上面的数据中,"ID:96"在冒号和数字之间没有空格.显然,我收到了这条警告信息: …
如何,在每行的第一列之后删除
$ cat tmp.txt
Name, Charles, James, Criss
Age, 21, 25, 23
Run Code Online (Sandbox Code Playgroud)
文件中可能有n列.
我需要像输出一样的输出
$ cat tmp.txt
Name, Charles James Criss
Age, 21 25 23
Run Code Online (Sandbox Code Playgroud)
在,不应该从第一列删除.
我想在字符串中用空格('')替换'with'一词。字符串中有很多'with'出现,我想保留第一个出现并替换所有其他出现。
我曾尝试使用gsub和regex,但似乎无法正确处理。
string <- gsub('with.*?','\\1 ',string)
Run Code Online (Sandbox Code Playgroud)
例如
string<- c('I want to, keep the first with with with with with with in this string')
Run Code Online (Sandbox Code Playgroud)
预期的结果:字符串<-我想,在此字符串中保留第一个