|
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.StringReader; |
30 | 30 | import java.util.Collection; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
31 | 33 |
|
32 | 34 | /** |
33 | 35 | * @author pavlidis |
@@ -190,28 +192,52 @@ public static boolean isLatinLetter( char c ) { |
190 | 192 | }; |
191 | 193 |
|
192 | 194 | /** |
193 | | - * Mimics the make.names method in R (character.c) to make valid variables names; we use this for column headers in |
194 | | - * some output files. This doesn't give the exact sames results as R; we avoid repeated '.'. |
| 195 | + * Mimics the {@code make.names} method in R (character.c) to make valid variables names; we use this for column |
| 196 | + * headers in some output files. |
| 197 | + * <p> |
| 198 | + * This was modified in 1.1.26 to match the behavior of R more closely, if not exactly. |
195 | 199 | * |
196 | | - * @param s |
| 200 | + * @param s a string to be made valid for R |
197 | 201 | * @return modified string |
198 | 202 | * @author paul |
199 | 203 | */ |
200 | | - public static String makeValidForR( String colName ) { |
201 | | - if ( colName == null ) { |
| 204 | + public static String makeValidForR( String s ) { |
| 205 | + if ( s == null ) { |
202 | 206 | return "NA"; |
203 | 207 | } |
204 | | - if ( colName.isEmpty() |
| 208 | + if ( s.isEmpty() |
205 | 209 | // starts with a non-letter or non-dot |
206 | | - || ( !Character.isAlphabetic( colName.charAt( 0 ) ) && colName.charAt( 0 ) != '.' ) |
| 210 | + || ( !Character.isAlphabetic( s.charAt( 0 ) ) && s.charAt( 0 ) != '.' ) |
207 | 211 | // dot followed by a digit |
208 | | - || ( colName.charAt( 0 ) == '.' && colName.length() > 1 && Character.isDigit( colName.charAt( 1 ) ) ) ) { |
209 | | - return "X" + colName.replaceAll( "[^A-Za-z0-9._]", "." ); |
| 212 | + || ( s.charAt( 0 ) == '.' && s.length() > 1 && Character.isDigit( s.charAt( 1 ) ) ) ) { |
| 213 | + return "X" + s.replaceAll( "[^A-Za-z0-9._]", "." ); |
210 | 214 | } |
211 | | - if ( StringUtils.equalsAny( colName, R_RESERVED_WORDS ) ) { |
212 | | - return colName + "."; |
| 215 | + if ( StringUtils.equalsAny( s, R_RESERVED_WORDS ) ) { |
| 216 | + return s + "."; |
213 | 217 | } |
214 | | - return colName.replaceAll( "[^A-Za-z0-9._]", "." ); |
| 218 | + return s.replaceAll( "[^A-Za-z0-9._]", "." ); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Mimics the {@code make.names} method in R when using with a vector of strings and the unique argument set to TRUE. |
| 223 | + * @author poirigui |
| 224 | + */ |
| 225 | + public static String[] makeValidForR( String[] strings ) { |
| 226 | + String[] result = new String[strings.length]; |
| 227 | + Map<String, Integer> counts = new HashMap<>(); |
| 228 | + for ( int i = 0; i < strings.length; i++ ) { |
| 229 | + String s = strings[i]; |
| 230 | + String rs = StringUtil.makeValidForR( s ); |
| 231 | + if ( counts.containsKey( rs ) ) { |
| 232 | + int count = counts.get( rs ); |
| 233 | + result[i] = rs + "." + count; |
| 234 | + counts.put( rs, count + 1 ); |
| 235 | + } else { |
| 236 | + result[i] = rs; |
| 237 | + counts.put( rs, 1 ); |
| 238 | + } |
| 239 | + } |
| 240 | + return result; |
215 | 241 | } |
216 | 242 |
|
217 | 243 | /** |
|
0 commit comments