Hi everyone!
How do you count letters with R?
Our participants had to name toys. We wanted to know wether the toys the participants had seen more often had longer names than toys seen less.
So, here we are. Our professor told us that it's easy to let R count the letters (e.g. the participant named a toy "malm" and we want R to tell us that it has 4 letters). So far google wasn't very helpful. Does anybody know how to do that?
Greetings from Germany!
Try this
toy1 <- "girrafe"
toy2 <- "anaconda"
toy3 <- "hippopotamus"
nchar(toy1)
[1] 7
hope this helps
Thank you very much for the quick response!
This is how our data looks like.
I need every single name to be replaced with a number and it doesn't work with nchar to count the entire table. But thank you so far!
You can try doing something like this:
dataset %>%
dplyr::mutate_if(is.character, nchar)
However, it's better to provide us with a repr oducible ex ample (reprex) illustrating your issue. Take a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
2 Likes
Try this:
library(data.table)
test <- setDT(Pfad1) #if you are using dataframe
test <- test[,lapply(.SD, nchar)]
That should do it
To test it first:
library(data.table)
set.seed(12)
test<-data.table(NULL)
for(i in 1:10){
test<-rbind(test,data.table(a=i,b=paste0(letters[sample(1:i)],collapse = "")))
}
#Everything
test<-test[,lapply(.SD, nchar)]
Hope that helps
To keep the original names, you can also modify @mishabalyasin 's approach as follows.
library(tidyverse)
starwars %>%
select(name:eye_color) %>%
mutate_at(vars(ends_with('color')),
list(len = ~str_length(.)))
#> # A tibble: 87 x 9
#> name height mass hair_color skin_color eye_color hair_color_len
#> <chr> <int> <dbl> <chr> <chr> <chr> <int>
#> 1 Luke… 172 77 blond fair blue 5
#> 2 C-3PO 167 75 <NA> gold yellow NA
#> 3 R2-D2 96 32 <NA> white, bl… red NA
#> 4 Dart… 202 136 none white yellow 4
#> 5 Leia… 150 49 brown light brown 5
#> 6 Owen… 178 120 brown, gr… light blue 11
#> 7 Beru… 165 75 brown light blue 5
#> 8 R5-D4 97 32 <NA> white, red red NA
#> 9 Bigg… 183 84 black light brown 5
#> 10 Obi-… 182 77 auburn, w… fair blue-gray 13
#> # … with 77 more rows, and 2 more variables: skin_color_len <int>,
#> # eye_color_len <int>
Created on 2019-06-18 by the reprex package (v0.3.0.9000)
1 Like
system
Closed
July 9, 2019, 11:44am
8
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.