caio.valente14
caio.valente14

Reputation: 87

Why can't I extract only the first word of the fields of a column with loop for in r?


Can anyone help me with this simple task.
I'm getting started in r, and I can't understand why this part of code isn't running inside a loop for.
I've tried to use the function strsplit() out of the loop and it worked well, but I'm not succeding in running the code the way I would like to, inside a for loop, in a data frame.

Here is the code:

mpg <- ggplot2::mpg
df_sort <- data.frame(uni_model = sort(unique(mpg$model)))
df_sort$model1 <- ''
for (x in seq_along(df_sort$model)){
    df_sort[x, 'model1'] <- strsplit(df_sort[x, 'model'], ' ')    
}

Upvotes: 0

Views: 423

Answers (1)

Phil
Phil

Reputation: 8107

I would suggest learning more about the tidyverse, as it provides a nice framework to learn and apply R tools without having to deal with idiosyncracies of base R. The following code does what you wish using dplyr and stringr for string manipulation:

library(dplyr)
library(stringr)

mutate(df_sort, model1 = word(uni_model, 1))

                uni_model      model1
1             4runner 4wd     4runner
2                      a4          a4
3              a4 quattro          a4
4              a6 quattro          a6
5                  altima      altima

etc...

Note that you don't need to use a for loop because R vectorizes by default. That is, any function you apply to a vector will by default be applied to each element of that vector.

Using base R, borrowing from here:

df_sort$model1 <- sapply(strsplit(df_sort$uni_model, "\\s"), `[`, 1)

Upvotes: 1

Related Questions