Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through columns and add string lengths as new columns

Tags:

loops

r

I have a data frame with a number of columns, and would like to output a separate column for each with the length of each row in it.

I am trying to iterate through the column names, and for each column output a corresponding column with '_length' attached.

For example col1 | col2 would go to col1 | col2 | col1_length | col2_length

The code I am using is:

df <- data.frame(col1 = c("abc","abcd","a","abcdefg"),col2 = c("adf qqwe","d","e","f"))

for(i in names(df)){
  df$paste(i,'length',sep="_") <- str_length(df$i)
 }

However this throws and error:

invalid function in complex assignment.

Am I able to use loops in this way in R?

like image 927
Zfunk Avatar asked Aug 27 '13 10:08

Zfunk


3 Answers

You need to use [[, the programmatic equivalent of $. Otherwise, for example, when i is col1, R will look for df$i instead of df$col1.

for(i in names(df)){
  df[[paste(i, 'length', sep="_")]] <- str_length(df[[i]])
}
like image 174
flodel Avatar answered Nov 01 '22 15:11

flodel


You can use lapply to pass each column to str_length, then cbind it to your original data.frame...

library(stringr)

out <- lapply( df , str_length )    
df <- cbind( df , out )

#     col1     col2 col1 col2
#1     abc adf qqwe    3    8
#2    abcd        d    4    1
#3       a        e    1    1
#4 abcdefg        f    7    1
like image 20
Simon O'Hanlon Avatar answered Nov 01 '22 15:11

Simon O'Hanlon


With dplyr and stringr you can use mutate_all:

> df %>% mutate_all(funs(length = str_length(.)))

     col1     col2 col1_length col2_length
1     abc adf qqwe           3           8
2    abcd        d           4           1
3       a        e           1           1
4 abcdefg        f           7           1
like image 7
m0nhawk Avatar answered Nov 01 '22 14:11

m0nhawk