[R] 2nd question about adding an additional variable along certain criteria in a dataframe

Sarah Goslee sarah.goslee at gmail.com
Wed Jun 26 17:49:35 CEST 2013


Hi,

Using your testframe and testvector (and thank you for the convenient
reproducible example):

# I used NA instead of "to be assigned"
testframe <- data.frame(testframe, landuse = "NA", stringsAsFactors=FALSE)

# checks membership in testvector
testframe[testframe$testcoordID %in% testvector, "landuse"] <- "gfgh"


Sarah

On Wed, Jun 26, 2013 at 5:36 AM, Jacqueline Oehri
<jacqueline.oehri at gmail.com> wrote:
> Hello everybody,
>
> I have a question concerning the work with my dataframe and i hope some of
> you can help me out:
>
> I have A data frame called "WWA" that looks like the "testframe"
> underneath:
>
> Additionally, i have a testvector, containing coordinate IDs:
> testvector <-c("503146","551154","557154")
>
>> testframe:
>
>        testcoordID  testcommunity  testaltitude    testSpeciesName
> 1      503146       Bournes        523.2           Bellis perennis
> 2      503146       Bournes        321.5           Cynosurus cristatus
> 3      557154       Bournes        654.1          Festuca pratensis
> 4      557154         Aigle        938.6          Bellis perennis
> 5      569226         Aigle        401.3          Bellis perennis
> 6      599246         Aigle        765.9          Prunella vulgaris
>
> I added an additional collumn:
>
>> testframe$testNCCR_land_use <- c("to be assigned")
>> testframe
>
> testcoordID testcommunity testaltitude     testSpeciesName
> testNCCR_land_use
> 1      503146       Bournes        523.2          Bellis
> perennis            to be assigned
> 2      503146       Bournes        321.5          Cynosurus cristatus   to
> be assigned
> 3      557154       Bournes        654.1          Festuca pratensis
> to be assigned
> 4      557154         Aigle           938.6           Bellis
> perennis            to be assigned
> 5      569226         Aigle           401.3           Bellis
> perennis            to be assigned
> 6      599246         Aigle           765.9           Prunella
> vulgaris         to be assigned
>
>
>
> -->QUESTION:
>  Now I need to compute a function, that checks for the coordID in every
> row, if the respective coordinateID is contained in the test-vector or not!
>
> 1) If the coordID in the row is contained in the testvector --> It should
> assign the name "gfgh" into the "testNCCR-land_use_collumn to this row!
>
> 2)If the coordID in the row is not contained in the testvector--> there
> should be done nothing and there will still be "to be assigned" in the
> testNCCR_land_use column.
>
> I tried something like this but it didnt work: Can somebody help me out?
> It says: "Error, trial to use a non-function" or something like this..
>
>
> for (i in 1:6(testframe)){
> +   if ((length(intersect(testvector, testframe$testcoordID[i]))) == 1){
> +     testframe$testNCCR_land_use[i]<-("testgfgh")}
> + }
> Fehler: Versuch eine Nicht-Funktion anzuwenden
>
>
> Thank you veery much for your help!!!!
>
>
>
> (I built the testframe up like this:
>
> testcoordID <-
> c(as.integer("503146"),as.integer("503146"),as.integer("557154"),as.integer("557154"),as.integer("569226"),as.integer("599246"))
> testcommunity <-factor(c("Bournes","Bournes","Bournes", "Aigle", "Aigle",
> "Aigle"))
> testaltitude <- c(523.2,321.5,654.1,938.6,401.3,765.9)
> testSpeciesName <-c( "Bellis perennis",
>                     "Cynosurus cristatus",
>                     "Festuca pratensis",
>                    "Bellis perennis",
>                    "Bellis perennis",
>                     "Prunella vulgaris")
> testframe <- data.frame(testcoordID,testcommunity,testaltitude,
> testSpeciesName)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org



More information about the R-help mailing list