如何根据列的名称将数据框列中的所有值相乘?

时间:2017-10-09 15:10:53

标签: r

我有一个包含以下数据的数据框:

Species Aug-16  Sep-16  Aug-17  Sep-17
Cat 740 69  758 443
Dog 783 559 230 143
Lizard  294 965 718 938

我想将数据框列中的每个变量乘以0.5 IF ,该列的名称中包含单词"Sep"

4 个答案:

答案 0 :(得分:2)

在基地R:

df1[grepl("Sep",names(df1))] <- 0.5* df1[grepl("Sep",names(df1))]

答案 1 :(得分:0)

我只看到'9月',如果'9月'在9月到9月改变str_detect功能

library(stringr)

df[,str_detect(names(df),'Sep')]=df[,str_detect(names(df),'Sep')]*0.5
df
  Species Aug.16 Sep.16 Aug.17 Sep.17
1     Cat    740   34.5    758  221.5
2     Dog    783  279.5    230   71.5
3  Lizard    294  482.5    718  469.0

答案 2 :(得分:0)

您可以为“Sep”执行此操作,如下所示:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath group: 'pl.allegro.tech.build', name: 'axion-release-plugin', version: '1.7.1'
        classpath 'org.hidetake:gradle-ssh-plugin:1.1.3'
    }
}

plugins {
    id "nebula.provided-base" version "3.0.3"
}

apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'groovy'
apply plugin: 'osgi'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'signing'
apply plugin: 'pl.allegro.tech.build.axion-release'
apply plugin: 'nebula.optional-base'
apply from: 'gradle/dist.gradle'

group = 'org.mnode.ical4j'
description = '''
A Java library for reading and writing iCalendar (*.ics) files
'''

sourceCompatibility = 1.7
targetCompatibility = 1.7
ext {
    slf4jVersion = '1.7.10'
}

repositories {
    mavenCentral()
}

dependencies {
    api "org.slf4j:slf4j-api:$slf4jVersion",
        'commons-codec:commons-codec:1.9',
        'org.apache.commons:commons-lang3:3.3.2',
        'org.apache.commons:commons-collections4:4.0',
        'org.threeten:threetenbp:1.3.3'

    compile 'org.codehaus.groovy:groovy-all:2.3.2', optional

    compileOnly 'biz.aQute.bnd:bndlib:2.3.0'

    testImplementation 'org.codehaus.groovy:groovy-all:2.3.2',
        'org.spockframework:spock-core:0.7-groovy-2.0',
        'commons-io:commons-io:2.4',
        'org.ccil.cowan.tagsoup:tagsoup:1.2.1',
        "org.slf4j:slf4j-log4j12:$slf4jVersion"
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled false
    }
}

javadoc {
    if (JavaVersion.current().isJava8Compatible()) {
        options.addStringOption('Xdoclint:none', '-quiet')
    }
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allSource
    classifier = 'sources'
}

jar {
    manifest {
        instruction 'Import-Package', 'groovy.*;resolution:=optional, org.codehaus.groovy*;resolution:=optional, *'
    }
}

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}

signing {
    required { isReleaseVersion && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

scmVersion {
    tag {
        prefix = 'ical4j'
    }
    versionCreator 'versionWithBranch'
    branchVersionCreator = [
        'master': 'simple'
    ]
}
version = scmVersion.version

ext {
    isReleaseVersion = !version.endsWith("SNAPSHOT")
}

task writePom {
    doLast {
        pom {
            project {
                inceptionYear '2008'
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }
            }
        }.writeTo("pom.xml")
    }
}

答案 3 :(得分:0)

dplyr选项:

library(dplyr)

df <- structure(list(Species = c("Cat", "Dog", "Lizard"), 
                     `Aug-16` = c(740L, 783L, 294L), 
                     `Sep-16` = c(69L, 559L, 965L), 
                     `Aug-17` = c(758L, 230L, 718L), 
                     `Sep-17` = c(443L, 143L, 938L)), 
                .Names = c("Species", "Aug-16", "Sep-16", "Aug-17", "Sep-17"), 
                class = "data.frame", row.names = c(NA, -3L))

df2 <- df %>% mutate_at(vars(starts_with('Sep')), funs(. * .5))

df2
#>   Species Aug-16 Sep-16 Aug-17 Sep-17
#> 1     Cat    740   34.5    758  221.5
#> 2     Dog    783  279.5    230   71.5
#> 3  Lizard    294  482.5    718  469.0