我想使用R:
提取以下模式当我有一个字符串“a | c,d”时,我想要提取:
Module Module1
Dim MenuList As New List(Of String)
Dim aTimer As New System.Timers.Timer
Const marqueeText As String = "The quick brown fox... "
Dim sb As New System.Text.StringBuilder
Dim direction As Boolean = False
Sub PrintMenu(highlight As Integer, left As Integer, top As Integer)
Dim Nickvektor() As Integer = {1, 2, 3, 4, 5}
For I = 0 To MenuList.Count - 1
Console.CursorLeft = left
Console.CursorTop = top + I
If I = highlight Then
Console.Write("{0}", "[" & Nickvektor(I) & "]")
Else
Console.Write(MenuList(I))
End If
Next
End Sub
Sub Main()
Console.CursorVisible = False
aTimer.AutoReset = True
aTimer.Interval = 100 '1/10 second
AddHandler aTimer.Elapsed, AddressOf tick
Dim x As Integer = 0
Dim Nickvektor() As String = {" "}
For counter As Integer = 0 To 0
Do
For Each s In Nickvektor
MenuList.Add(s)
Next
x += 1
Loop Until x = 5
Next
Console.SetCursorPosition(10, 16)
Console.Write("[ ]")
Dim CurrentItem As Integer = 0
Dim CurrentKey As ConsoleKey
While CurrentKey <> ConsoleKey.Enter
If CurrentItem = 2 Then ' Zero can be used to show the marquee output prompt
aTimer.Start() ' With a change to two or four the timer can be stoped:
'Else
'If aTimer.Enabled Then
' aTimer.Stop()
'End If
End If
PrintMenu(CurrentItem, 10, 10)
CurrentKey = Console.ReadKey(True).Key
Select Case CurrentKey
Case ConsoleKey.DownArrow
CurrentItem += 1
Case ConsoleKey.UpArrow
CurrentItem -= 1
End Select
CurrentItem = (CurrentItem + MenuList.Count) Mod MenuList.Count
End While
End Sub
Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
If sb.Length = 0 Then sb.Append(marqueeText)
If direction Then
sb.Insert(0, sb(sb.Length - 1))
sb.Remove(sb.Length - 1, 1)
Else
sb.Append(sb(0))
sb.Remove(0, 1)
End If
Console.CursorVisible = False
Console.CursorLeft = 20
Console.CursorTop = 12 ' For the first Element CursorTop=10, fort he third 12
Console.Write("{0}", sb.ToString)
End Sub
End Module
其他例子:
A | C:
a,d
c,d
x | y | z,d:
a
c
A,B:
x,d
y,d
z,d
我用gsub |看到了意味着一个替代角色,但我似乎无法实现这个以获得我想要的结果。
答案 0 :(得分:2)
这是一个可以做你想要的功能(使用辅助函数来实现折叠更加简单)。
pastelist <- function(..., sep=",", collapse=" ")
do.call("paste", c(as.list(...), sep=sep, collapse=collapse))
textexpand <- function(x) {
sapply(Map(expand.grid, lapply(strsplit(x, ","), strsplit, "\\|")), pastelist)
}
textexpand("a|c,d")
# [1] "a,d c,d"
textexpand("a|c")
# [1] "a c"
textexpand("x|y|z,d")
# [1] "x,d y,d z,d"
textexpand("a,b")
# [1] "a,b"
答案 1 :(得分:0)
这是你在找什么?
string <- c("a|c, d","x|y|z","ddwah")
gsub(pattern = "a|b|c",replacement ="" ,x = string)
答案 2 :(得分:0)
读入数据并使用separate_rows
展开它。最后确保列按所需顺序排列。
library(dplyr)
library(tidyr)
input <- c("a|c, d", "x|y|z, e", "a, b")
input %>%
read.csv(text = ., header = FALSE) %>%
separate_rows(V1) %>%
select(V1, V2)
,并提供:
V1 V2
1 a d
2 c d
3 x e
4 y e
5 z e
6 a b