R中串联串起来的字符串

时间:2018-01-24 06:06:13

标签: r string

假设我在for循环中构建mystring,如下所示:

mystring = ""
colorIndex = 17
for(i in 1:ncol(myTable)){

     mystring = paste(mystring, paste("$('td:eq(",i, ")', nRow).attr('title', full_text);", sep = ""))
     mystring = paste(mystring, paste("$('td:eq(",i,")', nRow).css('cursor', 'pointer');", sep = ""))
     mystring = paste(mystring, "if(aData[",colorIndex,"] == 0){
                                      $(nRow).css('background-color','#f8f8ff')
                                  }else if(aData[",colorIndex,"]==1){
                                      $(nRow).css('background-color','#9EFAC5')
                                  }else{
                                      $(nRow).css('background-color','#FAF99E')
                                  };", sep ="")

}

现在,假设我的桌子有60列。我试图找出最简单的方法来做到这一点。我是否需要制作一个带有特殊字符的大字符串然后将字符格式化?如何有效地迭代我是扔我。但是,考虑到R对字符串的缓慢程度,我宁愿不在循环中这样做。

2 个答案:

答案 0 :(得分:1)

也许您可以使用import java.util.Scanner; public class test { public static void main(String[] args) { String gender; Scanner scan = new Scanner(System.in); System.out.println("What is your gender, male of female?"); gender = scan.nextLine(); if( gender == "f"||gender =="F"||gender =="female"||gender =="Female"||gender =="FEMALE") { System.out.println("A female" ); } if( gender == "m"||gender =="M"||gender =="male"||gender =="Male"||gender =="MALE") { System.out.println("A male."); } } } 来实现这一点,因为它可以使事情看起来更清晰,并提前将组合放在数据框中,这样您就不需要循环:

glue

答案 1 :(得分:1)

你根本不需要循环,因为paste是矢量化的:

i <- 1:ncol(myTable)

yourstring <- 
  paste(
    paste0(
    paste0(" ", "$('td:eq(",i, ")', nRow).attr('title', full_text);"),
    " ",
    paste0("$('td:eq(",i,")', nRow).css('cursor', 'pointer');"),
    "if(aData[",colorIndex,"] == 0){
                                   $(nRow).css('background-color','#f8f8ff')
                               }else if(aData[",colorIndex,"]==1){
                                   $(nRow).css('background-color','#9EFAC5')
                               }else{
                                   $(nRow).css('background-color','#FAF99E')
                               };"
    ), 
    collapse = "")