How to combine values within a variable

Dear all,

Newbie here, please bear in mind, if I come off somewhat confusing.

I have a question regarding the dataset I'm working on.

I have a single variable with 40 unique values. The issue is that some of these values should be merged into a single one. For an example I have a value called "banana" another one called "apple" and a last one called "grape" and I want to merge them into a value called "fruit".

How would I go about doing this?

I appreciate any help and thank you in advance!

Could you please help us help you by providing a reproducible example?
reprex dos and don'ts
reprex FAQ

Or perhaps at least a data sample that you're talking about?

Dear Taraas,

As the image shows I have 40 values. Some of these like "S", "S, S", "S, EL", "Udpeget af S, S", "S, RV" I want to merge these into a single value, as the observations are to be treated as a single value/category.

Hope it makes sense :slight_smile:

This is not a reprex, but we'll move past this for now.

Do you mean something like this?

x <- c("a", "b", "c")
[1] "a" "b" "c"

Now concat all

paste0(x, collapse = "")
[1] "abc"

Something like that, but more in the sense that a, b, c becomes a.

If that makes sense?

Not really :woozy_face:
Do you just want the first value of your vector?

No, I'm sorry for being a pain the a** - I'm more of a Stata kind-of guy, so this is quite confusing to me :smile:

But thanks a lot for your effort mate!

Don't worry, that's what the forum is for.
You just need to state clearly what you want. Again, reprexes help a lot.
reprex dos and don'ts
reprex FAQ

If you could create a sample input vector and then show us the desired outcome, we could work together to figure out how to get there.

This will get the first value (up to the first comma) for every element in the vector x. Is that what you are looking for?

x <- c("a, b, c", "a", "b", "a, c")
gsub("^(.*?),.*$", "\\1", x)
#> [1] "a" "a" "b" "a"

Created on 2018-12-10 by the reprex package (v0.2.0).

For reference, please see FAQ: What's a reproducible example (`reprex`) and how do I do one? because it generally help to have a bit more to go on. In this case, I'll have to make a guess about what kind of object your variable is, and I'm going to assume a list

shopping <- c("apples","lettuce","soap","banana", "tofu", "grape")
> new_shop <- recode(shopping, apples = "Fruit")
> new_shop <- recode(new_shop, banana = "Fruit")
> new_shop <- recode(new_shop, grape = "Fruit")
> new_shop
[1] "Fruit"   "lettuce" "soap"    "Fruit"   "tofu"    "Fruit"  
1 Like

Thank you all for your time and effort!

Yes Tecnocrat, that is exactly it!

I do apologize for the guessing game I've thrown you all in :smiley:

Glad that set you back on track. This community exists to help R users at all levels solve problems and learn enough to be able to help themselves and eventually help others.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.