I have this type of dataset
DF
V1 V2 V3
1. A AAA B
2. BBB B CC
3. C BB CCC
And I would like to select the longest string from DF and put it into the new column WINNER like this:
DF
V1 V2 V3 WINNER
1. A AAA B AAA
2. BBB B CC BBB
3. C BB CCC CCC
I have tried
mutate( WINNER = select(which.max (c(nchar(V1), nchar(V2), nchar(V3)))
but it works only for numeric values. I would prefer dplyr solution.
To find the maximum string length by column in the given dataframe, first, nchar() function is called to get the length of all the string present in the particular column of the dataframe, and then the max() function must be called to get the maximum value of the length of the string generated by the nchar() function.
Hi, Use nrow() and ncol() to determine the number of rows and columns of a data frame.
To get every largest element, in linear time, you have to do m=max(map(len,xs)); [x for x in xs if len(x) == m] .
df$winner <-
Reduce(function(x, y) ifelse(nchar(y) > nchar(x), y, x), df)
df
# V1 V2 V3 winner
# 1: A AAA B AAA
# 2: BBB B CC BBB
# 3: C BB CCC CCC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With