How to abbreviate long names in a dataframe for R?

后端 未结 1 1866
天涯浪人
天涯浪人 2020-12-21 08:44

I\'m working with a dataframe that has really long names that is more than 25 characters. I\'m trying to make a bar graph (with plotly) with all of these organizations name,

相关标签:
1条回答
  • 2020-12-21 09:03

    dataframe.name$abbr is a vector of all abbreviations in the dataframe, not just a single name.

    It is the reason all entries in dataframe.name$abbr are being set to NA; the last name is in the dataframe is 25 characters or less, so all entries in dataframe.name$abbr are assigned NA.

    @brettljausn has a decent suggestion: just do away with the NAs completely and only truncate where the character count exceeds 25.

    Something like this should work a treat:

    dataframe.name$abbrv <- substring( dataframe.name$org_name, 0, 25 )
    

    I would try to use abbreviate first though:

    dataframe.name$abbrv <- abbreviate( dataframe.name$org_name )
    
    0 讨论(0)
提交回复
热议问题