@@ -186,11 +186,6 @@ public static boolean isLatinLetter( char c ) {
186186 return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' );
187187 }
188188
189- private static final String [] R_RESERVED_WORDS = {
190- "if" , "else" , "repeat" , "while" , "function" , "for" , "in" , "next" , "break" ,
191- "TRUE" , "FALSE" , "NULL" , "Inf" , "NaN" , "NA" , "NA_integer_" , "NA_real_" , "NA_character_" , "NA_complex_" ,
192- };
193-
194189 /**
195190 * Mimics the {@code make.names} method in R (character.c) to make valid variables names; we use this for column
196191 * headers in some output files.
@@ -200,8 +195,63 @@ public static boolean isLatinLetter( char c ) {
200195 * @param s a string to be made valid for R
201196 * @return modified string
202197 * @author paul
198+ * @deprecated use {@link #makeNames(String[], boolean)} instead
203199 */
204200 public static String makeValidForR ( String s ) {
201+ return makeNames ( s );
202+ }
203+
204+ /**
205+ * Mimics the {@code make.names} method in R when using with a vector of strings and the unique argument set to TRUE.
206+ * @author poirigui
207+ * @deprecated use {@link #makeNames(String[], boolean)} instead
208+ */
209+ @ Deprecated
210+ public static String [] makeValidForR ( String [] strings ) {
211+ return makeNames ( strings , true );
212+ }
213+
214+ /**
215+ * Mimics the {@code make.names} method in R.
216+ * @param strings a list of strings to be made valid for R
217+ * @param unique if true, will ensure that the names are unique by appending a number to duplicates as per
218+ * {@link #makeUnique(String[])}
219+ * @author poirigui
220+ */
221+ public static String [] makeNames ( String [] strings , boolean unique ) {
222+ String [] result = new String [strings .length ];
223+ if ( unique ) {
224+ Map <String , Integer > counts = new HashMap <>();
225+ for ( int i = 0 ; i < strings .length ; i ++ ) {
226+ String s = strings [i ];
227+ String rs = makeNames ( s );
228+ if ( counts .containsKey ( rs ) ) {
229+ int count = counts .get ( rs );
230+ result [i ] = rs + "." + count ;
231+ counts .put ( rs , count + 1 );
232+ } else {
233+ result [i ] = rs ;
234+ counts .put ( rs , 1 );
235+ }
236+ }
237+ } else {
238+ for ( int i = 0 ; i < strings .length ; i ++ ) {
239+ result [i ] = makeNames ( strings [i ] );
240+ }
241+ }
242+ return result ;
243+ }
244+
245+ private static final String [] R_RESERVED_WORDS = {
246+ "if" , "else" , "repeat" , "while" , "function" , "for" , "in" , "next" , "break" ,
247+ "TRUE" , "FALSE" , "NULL" , "Inf" , "NaN" , "NA" , "NA_integer_" , "NA_real_" , "NA_character_" , "NA_complex_" ,
248+ };
249+
250+ /**
251+ * Mimics the {@code make.names} method in R for a single string.
252+ * @author paul
253+ */
254+ public static String makeNames ( String s ) {
205255 if ( s == null ) {
206256 return "NA" ;
207257 }
@@ -218,32 +268,11 @@ public static String makeValidForR( String s ) {
218268 return s .replaceAll ( "[^A-Za-z0-9._]" , "." );
219269 }
220270
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 ;
241- }
242-
243271 /**
244272 * Mimics the {@code make.unique} method in R.
245273 * <p>
246274 * Duplicated values in the input array will be suffixed with a dot and a number, starting from 1.
275+ * @author poirigui
247276 */
248277 public static String [] makeUnique ( String [] strings ) {
249278 Map <String , Integer > counts = new HashMap <>();
0 commit comments