R中矩阵中的字符串操作

问题描述

我有一个像这样的矩阵

B = matrix( 
  c("< 5","< 5","5 (2.5-7.5)","7 (5-9)"),nrow=3,ncol=2) 

我要用“

df$ad_id_no0<-which[df$ad_id>0] # this gives you a vector where all rows with a value above 0 are represented.

new_vector_no0<-df$ad_id[df$ad_id_no0] #this gives you the vector with all the above 0 values

有什么想法吗?

解决方法

提取第一个数字,将其转换为数字,然后用<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/DesignerItem.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <StackPanel Background="Gray" Grid.RowSpan="2"> <TextBlock Text="Shapes" FontSize="18" TextAlignment="Center" /> <Button Content="Initial Node" /> <Button Content="Final Node" /> <Button Content="Line" /> <Button Content="Action" /> <Button Content="Decision Node" /> <Button Content="Actor" /> <Button Content="Class" Command="{Binding Path=CreateRectangle}"/> <Button Content="Use Case" Command="{Binding Path = CreateUseCase}"/> </StackPanel> <Grid Grid.Column="2"> <ItemsControl ItemsSource="{Binding Path= BaseShapeViewModels}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.Resources> <DataTemplate DataType="{x:Type viewModel:EllipseViewModel}"> <ContentControl Selector.IsSelected="True" Style="{StaticResource DesignerItemStyle}"> <Ellipse Fill="{Binding Fill}" IsHitTestVisible="False"/> </ContentControl> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:RectangleViewModel}"> <ContentControl Selector.IsSelected="True" Style="{StaticResource DesignerItemStyle}"> <Rectangle Fill="{Binding Fill}" IsHitTestVisible="False"/> </ContentControl> </DataTemplate> </ItemsControl.Resources> <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="Canvas.Left" Value="{Binding Left,Mode=TwoWay}"/> <Setter Property="Canvas.Top" Value="{Binding Top,Mode=TwoWay}"/> <Setter Property="Width" Value="{Binding Width,Mode=TwoWay}"/> <Setter Property="Height" Value="{Binding Height,Mode=TwoWay}"/> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </Grid> </Grid> 替换小于5的数字。

webDriver.get(getTerminalUrl());
WebDriverWait wait = new WebDriverWait(webDriver,60);

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Sign In']")));
WebElement login = webDriver.findElement(By.xpath("//*[text()='Sign In']"));
login.click();

提取第一个数字并将其转换为数字的快捷方式是使用"<5"

A[as.numeric(sub('(\\d+).*','\\1',A)) < 5] <- '< 5'
A

#      [,1]  [,2]         
#[1,] "< 5" "< 5"        
#[2,] "< 5" "5 (2.5-7.5)"
#[3,] "< 5" "7 (5-9)"    
,

使用substr()提取每个矩阵元素的第一个字符。只要是数字,您就可以通过as.numeric()将其转换为一个数字。

A[as.numeric(substr(A,1,1))<5] <- "<5"
,

1)阅读表

使用read.table获取给定向量firstNo的每个单元格中的第一个数字。然后使用replace将这些单元格替换为< 5

保留原始输入A,这通常是希望使其更易于测试和调试的方法,但是如果您还是想覆盖它,则用{{1}替换第二行代码的左侧}。

不使用正则表达式,也不使用任何包。

A

给予:

firstNo <- read.table(text = A)[[1]]
B <- replace(A,firstNo < 5,"< 5")

B

问题中的示例输入虽然不需要,但是如果左括号后的文本可能不规则,那么您可能需要将 [,2] [1,] "< 5" "< 5" [2,] "< 5" "5 (2.5-7.5)" [3,] "< 5" "7 (5-9)" fill=TRUE参数添加到{{1 }}。

2)gsubfn

comment.char = "("类似于read.table,不同之处在于它将正则表达式中的捕获组(即正则表达式的括号部分)输入到第二个参数的公式符号表示的函数中,然后替换与函数的输出匹配。

gsubfn

给予:

gsub
,

如果只有5个选项,则无需提取并转换为数字:

即“ 0”或“ 1”或“ 2”或“ 3”或“ 4”

A[grep("^[0-4]",A)] <- "< 5"

replace(A,grep("^[0-4]",A),"< 5")

replace(A,startsWith("[0-4]","< 5")

结果

#      [,2]         
# [1,] "< 5" "< 5"        
# [2,] "< 5" "5 (2.5-7.5)"
# [3,] "< 5" "7 (5-9)"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...