mardi 26 janvier 2021

Change object names using if()

I am stuck on a question for an assignment for a base R class.

I need to use if() to change the names of several objects. Anything named af, aflb, afub, afwb, or afws will be changed to af_type. They are in the column LANDSCAPE.

Here is the code I have tried:

poppy <- read.csv("~/baseR-V2021.1EXP/data/exercise_dat/bearclawpoppy.csv")

af_type <- if(any(poppy$LANDSCAPE == "af", "aflb", "afub", "afwb", "afws"))"af_type"

af_type <- if(any(poppy$LANDSCAPE == "af", "aflb", "afub", "afwb", "afws")){"af_type"}

When these didn't work, I tried to simplify it:

af_type <- if(poppy$LANDSCAPE == "af")"af_type"

I get an error saying argument is of length zero.

Then I tried:

af_type <- ifelse(poppy$LANDFORM == "af", "af_type", "other")

Return labelled all objects "other".

Here is a screenshot to show what the dataset looks like.

enter image description here

I would appreciate advice! Thank you

Aucun commentaire:

Enregistrer un commentaire