Difference between revisions of "Simple functions"

From kogic.kr
Line 3: Line 3:
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Input</span></h3>
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Input</span></h3>
  
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace">x &lt;- &quot;AAA@BBB&quot;</span></div>
+
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:11px"><span style="font-family:courier new,courier,monospace">x &lt;- &quot;AAA@BBB&quot;</span></span></div>
  
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Code</span></h3>
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Code</span></h3>
  
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace">split_pick &lt;- function(x , Div_str, Pick_num){<br />
+
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:11px"><span style="font-family:courier new,courier,monospace">split_pick &lt;- function(x , Div_str, Pick_num){<br />
 
&nbsp;&nbsp; &nbsp;return(as.character(as.data.table(strsplit(x, Div_str))[Pick_num]))&nbsp;<br />
 
&nbsp;&nbsp; &nbsp;return(as.character(as.data.table(strsplit(x, Div_str))[Pick_num]))&nbsp;<br />
}</span></div>
+
}</span></span></div>
  
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace">split_pick(x, &quot;@&quot;, 2)</span></div>
+
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:11px"><span style="font-family:courier new,courier,monospace">split_pick(x, &quot;@&quot;, 2)</span></span></div>
  
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Output</span></h3>
 
<h3 style="color:#aaa; font-style:italic"><span style="font-family:courier new,courier,monospace">Output</span></h3>
  
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-family:courier new,courier,monospace">&quot;BBB&quot;</span></div>
+
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;"><span style="font-size:11px"><span style="font-family:courier new,courier,monospace">&quot;BBB&quot;</span></span></div>
  
 
<p>&nbsp;</p>
 
<p>&nbsp;</p>

Revision as of 13:28, 27 November 2018

Split the elements of a character vector and select a element

Input

x <- "AAA@BBB"

Code

split_pick <- function(x , Div_str, Pick_num){

    return(as.character(as.data.table(strsplit(x, Div_str))[Pick_num])) 

}
split_pick(x, "@", 2)

Output

"BBB"