R:从列中的每个字符值中提取最高数值

时间:2017-12-26 18:23:47

标签: r

我在数据框中有一个字符字段,其中包含数字,例如(0.5,3.5,7.8,2.4)。

对于每个记录,我试图从字符串中提取最大值并将其放入新列中。

e.g。

SwipeRefreshLayout

所需的输出是:

x  csi
1  0.5, 6.7, 2.3   
2  9.5, 2.6, 1.1
3  0.7, 2.3, 5.1
4  4.1, 2.7, 4.7

我已经进行了各种尝试......我的最新尝试是以下内容 - 它提供了整个列的最大csi分数,而不是单个行的csi数字......

x  csi            csi_max
1  0.5, 6.7, 2.3  6.7
2  9.5, 2.6, 1.1  9.5
3  0.7, 2.3, 5.1  5.1
4  4.1, 2.7, 4.7  4.7

谢谢

3 个答案:

答案 0 :(得分:6)

我们可以使用base R

data.frame

或者,在分离出“csi”后,可以使用read.table中的df1$csi_max <- do.call(pmax, read.table(text=df1$csi, sep=",")) 来完成此操作。列FlatCheckBox.CheckedFlatCheckBox.Unchecked

<UserControl x:Name="userControl" x:Class="Sync_Launcher.Controls.FlatCheckBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:controls="clr-namespace:Sync_Launcher.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="30"
         Background="Transparent" MouseLeftButtonDown="FlatCheckBox_OnMouseLeftButtonDown">
<Grid SnapsToDevicePixels="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding ActualHeight, ElementName=userControl}"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid Column="0">
        <Grid Name="tickHolderGridRoot" Margin="4">
            <Grid.LayoutTransform>
                <TransformGroup>
                    <RotateTransform x:Name="rotationTransform" Angle="0"/>
                </TransformGroup>
            </Grid.LayoutTransform>
            <Grid Name="tickHolderGrid" Margin="0,0">
                <Border Name="tickBorder" SnapsToDevicePixels="True" Opacity="0" BorderThickness="2,0,0,2" BorderBrush="#FF1CA36F" />
                <Border Name="overlayBorder" SnapsToDevicePixels="True" Opacity="1" BorderThickness="2,2,2,2" BorderBrush="#FF404D61" />
            </Grid>
        </Grid>
    </Grid>
    <Label 
        Grid.Column="1" 
        Content="{Binding Text, ElementName=userControl}" 
        VerticalContentAlignment="Center" 
        FontSize="16" 
        Padding="5,0" 
        FontStyle="{Binding FontStyle, ElementName=userControl}" 
        FontWeight="{Binding FontWeight, ElementName=userControl}"/>
</Grid>
<UserControl.Resources>
    <Duration x:Key="animationDuration">0:0:0.4</Duration>
    <KeyTime x:Key="animationEnd">0:0:0.4</KeyTime>
</UserControl.Resources>
<UserControl.Triggers>
    <EventTrigger SourceName="userControl" RoutedEvent="controls:FlatCheckBox.Checked">
        <BeginStoryboard>
            <Storyboard Timeline.DesiredFrameRate="60">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="rotationTransform" Storyboard.TargetProperty="Angle" Duration="{StaticResource animationDuration}">
                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                    <EasingDoubleKeyFrame KeyTime="{StaticResource animationEnd}" Value="-60">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <CircleEase EasingMode="EaseOut"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimation Storyboard.TargetName="overlayBorder" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="{StaticResource animationDuration}"/>
                <DoubleAnimation Storyboard.TargetName="tickBorder" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="{StaticResource animationDuration}"/>
                <ThicknessAnimation Storyboard.TargetName="tickHolderGrid" Storyboard.TargetProperty="Margin" From="0,0" To="0,2" Duration="{StaticResource animationDuration}"/>
                <ThicknessAnimation Storyboard.TargetName="tickHolderGridRoot" Storyboard.TargetProperty="Margin" From="4" To="2,0,2,4" Duration="{StaticResource animationDuration}"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger SourceName="userControl" RoutedEvent="controls:FlatCheckBox.Unchecked">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="rotationTransform" Storyboard.TargetProperty="Angle" Duration="{StaticResource animationDuration}">
                    <EasingDoubleKeyFrame KeyTime="0" Value="-60"/>
                    <EasingDoubleKeyFrame KeyTime="{StaticResource animationEnd}" Value="0">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <CircleEase EasingMode="EaseOut"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimation Storyboard.TargetName="overlayBorder" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="{StaticResource animationDuration}"/>
                <DoubleAnimation Storyboard.TargetName="tickBorder" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="{StaticResource animationDuration}"/>
                <ThicknessAnimation Storyboard.TargetName="tickHolderGrid" Storyboard.TargetProperty="Margin" From="0,2" To="0,0" Duration="{StaticResource animationDuration}"/>
                <ThicknessAnimation Storyboard.TargetName="tickHolderGridRoot" Storyboard.TargetProperty="Margin" From="2" To="4" Duration="{StaticResource animationDuration}"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</UserControl.Triggers>

答案 1 :(得分:3)

希望这有帮助!

df$csi_max <- sapply(df$csi, function(x) max(as.numeric(unlist(strsplit(as.character(x), split=",")))))

输出是:

  x           csi csi_max
1 1 0.5, 6.7, 2.3     6.7
2 2 9.5, 2.6, 1.1     9.5
3 3 0.7, 2.3, 5.1     5.1
4 4 4.1, 2.7, 4.7     4.7


#sample data
> dput(df)
structure(list(x = 1:4, csi = structure(c(1L, 4L, 2L, 3L), .Label = c("0.5, 6.7, 2.3", 
"0.7, 2.3, 5.1", "4.1, 2.7, 4.7", "9.5, 2.6, 1.1"), class = "factor")), .Names = c("x", 
"csi"), class = "data.frame", row.names = c(NA, -4L))


修改
正如@RichScriven所建议的那样,效率更高的方式可能是

df$csi_max <- sapply(strsplit(as.character(df$csi), ","), function(x) max(as.numeric(x)))

答案 2 :(得分:1)

使用包的解决方案。

library(splitstackshape)

dat$csi_max <- apply(cSplit(dat, "csi")[, -1], 1, max)
dat
#   x           csi csi_max
# 1 1 0.5, 6.7, 2.3     6.7
# 2 2 9.5, 2.6, 1.1     9.5
# 3 3 0.7, 2.3, 5.1     5.1
# 4 4 4.1, 2.7, 4.7     4.7

数据

dat <- read.table(text = "x  csi
1  '0.5, 6.7, 2.3'   
                  2  '9.5, 2.6, 1.1'
                  3  '0.7, 2.3, 5.1'
                  4  '4.1, 2.7, 4.7'",
                  header = TRUE, stringsAsFactors = FALSE)