找到并替换某种条件

时间:2016-08-24 23:39:28

标签: r

我有一些字符串错误地删除了“空格”。但我很幸运,我也有原始字符串,我希望用原始字符串替换更改的字符串。我的数据是:

#dts1

Id     changed   Original

1        cfd      a b c
2        abc      cf d

删除原始数据的所有空格。我希望我的程序转到已更改的列,例如找到 cfd ,并了解它是 cf d 并替换它。

所以,我理想的输出是

#output

  Id  changed 

   1    cf d
   2    a b c

非常感谢任何提示。

  dts1 <- structure(list(original = c("a b c", "cf d"), changed = c("cfd", 
  "abc")), .Names = c("original", "changed"), row.names = c(NA, 
  -2L), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

dts1$original[match(dts1$changed, gsub("\\s+","",dts1$original))] #[1] "cf d" "a b c" 中删除空格并将其匹配?

dts1 <- structure(list(original = c("a b c", "cf d"), changed = c("cfd", 
"abc")), .Names = c("original", "changed"), row.names = c(NA, 
-2L), class = "data.frame")

其中:

$key = '123';
$secret = '123';

$bucket = 'bck-users';
$keyname = $username;

try {
            //Create bucket for user
            $s3 = Aws\S3\S3Client::factory(array(
            'region' => 'eu-west-1',
            'version' => '2006-03-01',
            'credentials' => array(
              'key' => $key,
              'secret' => $secret,
                )
            ));

            $result = $s3->putObject(array(
              'Bucket' => $bucket,
              'Key'    => $keyname,
            ));

            echo $result['ObjectURL'] . "\n";

        }
        catch (Exception $e){
          echo $e->getMessage() . "\n";
        }

        //Copy default files to bucket
        $sourceBucket = 'bck-users';
        $sourceKeyname = 'default';
        $targetBucket = $username;
        $targetKeyname = 'default';             

        // Instantiate the client.
        $s3 = S3Client::factory(array(
            'region' => 'eu-west-1',
            'version' => '2006-03-01',
            'credentials' => array(
              'key' => $key,
              'secret' => $secret,);

        // Perform a batch of CopyObject operations.
        $batch = array();
        $batch[] = $s3->getCommand('CopyObject', array(
                'Bucket'     => $targetBucket,
                'Key'        => $targetKeyname,
                'CopySource' => $sourceBucket/$sourceKeyname,
            ));
        }
        try {
            $successful = $s3->execute($batch);
        } 
        catch (Exception $e){
          echo $e->getMessage() . "\n";
        }