我想用两个单词分割一个字符串:
s <- "PCB153 treated HepG2 cells at T18"
strsplit(s, split = <treated><at>)
Run Code Online (Sandbox Code Playgroud)
我应该写什么而不是<>?
我会得到:
"PCB153" "HepG2 cells" "T18"
Run Code Online (Sandbox Code Playgroud) 我有一个如下数据集:
Country Region Molecule Item Code
IND NA PB102 FR206985511
THAI AP PB103 BA-107603 / F000113361 / 107603
LUXE NA PB105 1012701 / SGP-1012701 / F041701000
IND AP PB106 AU206985211 / CA-F206985211
THAI HP PB107 F034702000 / 1010701 / SGP-1010701
BANG NA PB108 F000007970/25781/20009021
Run Code Online (Sandbox Code Playgroud)
我想基于ITEMCODE列中的字符串值进行拆分,/并为每个条目创建一个新行.
例如,所需的输出将是:
Country Region Molecule Item.Code
IND NA PB102 FR206985511
THAI AP PB103 BA-107603
THAI AP PB103 F000113361
THAI AP PB103 107603
LUXE NA PB105 1012701
LUXE NA PB105 SGP-1012701
LUXE NA …Run Code Online (Sandbox Code Playgroud) 我有矢量
length
# [1] 15,34, 12,24, 225,
# Levels: 12,24, 15,34, 225,
Run Code Online (Sandbox Code Playgroud)
我想用逗号分隔它们,最终列出这些值
尝试:
strsplit(length, ",")
Run Code Online (Sandbox Code Playgroud)
但不断收到错误消息
Error in strsplit(length, ",") : non-character argument
Run Code Online (Sandbox Code Playgroud) 我试图将一个字符串分成1,2和3段.
例如,我目前有这个:
$str = 'test';
$arr1 = str_split($str);
foreach($arr1 as $ar1) {
echo strtolower($ar1).' ';
}
Run Code Online (Sandbox Code Playgroud)
哪个适用于1个字符拆分,我得到:
t e s t
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试:
$arr2 = str_split($str, 2);
Run Code Online (Sandbox Code Playgroud)
我明白了:
te st
Run Code Online (Sandbox Code Playgroud)
有没有办法可以输出这个?:
te es st
Run Code Online (Sandbox Code Playgroud)
然后还有3个这样的字符?
tes est
Run Code Online (Sandbox Code Playgroud) 我试图在数据框内将字符向量分成三个不同的向量.
我的数据类似于:
> df <- data.frame(filename = c("Author1 (2010) Title of paper",
"Author2 et al (2009) Title of paper",
"Author3 & Author4 (2004) Title of paper"),
stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
我想这3个信息(拆分authors,year,title)分成三个不同的列,所以,这将是:
> df
filename author year title
1 Author1 (2010) Title1 Author1 2010 Title1
2 Author2 et al (2009) Title2 Author2 et al 2009 Title2
3 Author3 & Author4 (2004) Title3 Author3 & Author4 2004 Title3
Run Code Online (Sandbox Code Playgroud)
我习惯在3个元素的向量中strsplit分割每个filename元素:
df$temp <- strsplit(df$filename, " …Run Code Online (Sandbox Code Playgroud) 假设我有一个包含一些我希望根据正则表达式拆分的字符的向量.
更确切地说,我想基于逗号分隔字符串,然后是空格,然后是大写字母(根据我的理解,regex命令看起来像这样:( /(, [A-Z])/g当我在这里尝试它时工作正常)).
当我尝试实现这一点时r,regex似乎不起作用,例如:
x <- c("Non MMF investment funds, Insurance corporations, Assets (Net Acquisition of), Loans, Long-term original maturity (over 1 year or no stated maturity)",
"Non financial corporations, Financial corporations other than MFIs, insurance corporations, pension funds and non-MMF investment funds, Assets (Net Acquisition of), Loans, Short-term original maturity (up to 1 year)")
strsplit(x, "/(, [A-Z])/g")
[[1]]
[1] "Non MMF investment funds, Insurance corporations, Assets (Net Acquisition of), …Run Code Online (Sandbox Code Playgroud) 我正在编写一个R脚本,并希望定义一个变量,以便在绘图注释中用作文件名的一部分.我以为我会使用strsplit()函数.这是我的代码和输出:
infile = "ACC_1346.table.txt"
x = strsplit(infile, ".")
class(infile)
[1] "character"
class(x)
[1] "list"
str(x)
List of 1
$ : chr [1:18] "" "" "" "" ...
x[[1]]
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Run Code Online (Sandbox Code Playgroud)
我预计最终的输出是:
[1] "ACC_1346" "table" "txt"
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
这是一个字符串:
[1] "5 15 3 23 11 59 44.7 -.263226218521e-03 .488853402202e-11 .000000000000e+01"
Run Code Online (Sandbox Code Playgroud)
我需要将前 7 个数字保留在一起的某些空格将其拆分,如下所示:
[1] "5 15 3 23 11 59 44.7" "-.263226218521e-03" ".488853402202e-11" ".000000000000e+01"
Run Code Online (Sandbox Code Playgroud)
因此,我尝试使用前瞻正则表达式按后跟点或减号的空格进行拆分:
strsplit(mystring,"(?=[-.]) +",perl=T)
Run Code Online (Sandbox Code Playgroud)
或者
strsplit(nraw,"(?=[-.])\\s+",perl=T)
Run Code Online (Sandbox Code Playgroud)
但是正则表达式在任何地方都不匹配,输出原始字符串。
我究竟做错了什么?
我有2个因子列,我想创建第三列,它告诉我第二列是什么,第一列没有.它与这篇文章非常相似,但是我从df使用setdiff()函数到使用函数时遇到了麻烦.
例如:
library(dplyr)
y1 <- c("a.b.","a.","b.c.d.")
y2 <- c("a.b.c.","a.b.","b.c.d.")
df <- data.frame(y1,y2)
Run Code Online (Sandbox Code Playgroud)
列y1有a.b.和列y2有a.b.c..我想要一个三分之一的列返回c.或只是c.
> df
y1 y2 col3
1 a.b. a.b.c. c.
2 a. a.b. b.
3 b.c.d. b.c.d.
Run Code Online (Sandbox Code Playgroud)
我认为这是应该的组合strsplit和setdiff,但我不能得到它的工作.
我试图将其转换factor为character,然后我尝试应用于strsplit()结果,但输出对我来说似乎很奇怪.它似乎在列表中创建了一个列表,这使得很难传递给它setdiff()
#convert factor to character
df <- df %>% mutate_if(is.factor, as.character)
lapply(df$y1,function(x)(strsplit(x,split = "[.]")))
> lapply(df$y1,function(x)(strsplit(x,split = "[.]")))
[[1]]
[[1]][[1]]
[1] …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数据集:
Old <- data.frame(
X1= c(
"AD=17795,54;ARL=139;DEA=20;DER=20;DP=1785",
"DP=4784;AD=4753,23;ARL=123;DEA=5;DER=5",
"ARL=149;AD=30,9;DEA=25;DER=25;DP=3077",
"AD=244,49;ARL=144;DEA=7;DER=7;DP=245"
))
X1
AD=17795,54;ARL=139;DEA=20;DER=20;DP=1785
DP=4784;AD=4753,23;ARL=123;DEA=5;DER=5
ARL=149;AD=30,9;DEA=25;DER=25;DP=3077
AD=244,49;ARL=144;DEA=7;DER=7;DP=245
Run Code Online (Sandbox Code Playgroud)
我想提取“;” AD=xxx,xx 的单独值比添加到新列:所需的输出是:
X1 X2
AD=17795,54;ARL=139;DEA=20;DER=20;DP=1785 17795,54
DP=4784;AD=4753,23;ARL=123;DEA=5;DER=5 4753,23
ARL=149;AD=30,9;DEA=25;DER=25;DP=3077 30,9
AD=244,49;ARL=144;DEA=7;DER=7;DP=245 244,49
Run Code Online (Sandbox Code Playgroud)
我试过了:
Old$X2<-mapply(
function(x, i) x[i],
strsplit(X1, ";"),
lapply(strsplit(X1, ";"), function(x) which(x == "AD="))
)
Run Code Online (Sandbox Code Playgroud)