如何在R中使用`+`符号合并字符串?

huangapple go评论143阅读模式
英文:

How to combine strings in R with the `+` sign?

问题

如果要翻译的话,以下是翻译好的部分:

我有一个if语句,如果传递给名为`case`的函数的参数长度大于1,那么数据框的第一个列名将是`case`的前两个值的组合:

for(i in 1:length(case)){
  if (length(case) > 1){
    colnames(scores)[1] = case[[i]] + case[[i+1]]
  } else {
    colnames(scores)[1] = case  
    
  }
}

问题出在case[[i]] + case[[i+1]]部分。例如,在Python中,如果你执行a + b,结果将是ab。但在这里,我不知道如何做到这一点。我只是希望如果上面的条件为真,scores的第一列将变为case[[i]] + case[[i+1]]

case只是一个包含字符串的向量,如case = c('CR', 'PD')。有时我传递它的值只有一个,有时更多。在这种情况下,我希望scores的第一列变成CRPD或类似的东西。

如果我尝试上面的代码,我会得到以下错误,当然这是因为CRPD是字符串,这里不会像Python那样工作:

Error in case[[i]] + case[[i + 1]] : 
  non-numeric argument to binary operator
英文:

I have an if statment, that if an argument that is passed to the function, called case, is longer than 1, then the first colname of the data frame would be the combination of the first two values of case:

  for(i in 1:length(case)){
    if (length(case) > 1){
      colnames(scores)[1] = case[[i]] + case[[i+1]]
    } else {
      colnames(scores)[1] = case  
      
    }
  }

the problem is with the case[[i]] + case[[i+1]] part. In python, for example, if you do a + b, the result would be ab. Here I don't know how to do that. All I want is that if the condition above is TRUE, the first column of scores would become case[[i]] + case[[i+1]].

case is just a vector that contains strings, like this case = c('CR','PD'). Sometimes I pass it with one value, sometimes more. In my case here I want the first column of scores to be CRPD or something like that.

If I tried the code above, I get this error, and that is of course because CR and PD are strings and here this won't work like in python:

Error in case[[i]] + case[[i + 1]] : 
  non-numeric argument to binary operator

答案1

得分: 0

以下是翻译好的部分:

首先,R中的字符串连接没有像Python中那样重载+运算符。相反,可以使用paste()(默认分隔符为" ")或paste0()(分隔符为"")来进行字符串连接,例如:paste0(case[[i]], case[[i+1]])

其次,你的for循环没有意义,如果我理解你的目标,它是不需要的。按照你的写法,它在每次迭代时都会覆盖第一列的列名,在最后一次迭代中,case[[i + 1]] 尝试访问一个大于向量总长度的索引,这在定义上是不存在的。

使用paste0()并且放弃for循环:

if (length(case) > 1) {
  colnames(scores)[1] <- paste0(case[[1]], case[[2]])
} else {
  colnames(scores)[1] <- case
}

head(scores)
      CRPD Gender treatment Cancer_Type CD4-T-cells
Pt1     PD   male  anti-PD1    Melanoma -0.07410987
Pt10    SD female  anti-PD1    Melanoma -0.09440127
Pt101   PR female  anti-PD1    Melanoma  0.04102849
Pt103   PD female  anti-PD1    Melanoma -0.16330295
Pt106   PD   male  anti-PD1    Melanoma -0.09424782
Pt11    PD female  anti-PD1    Melanoma -0.16731441

如果你的目标是将case所有元素连接在一起,而不考虑其长度,你可以摆脱if语句,只需执行以下操作:

colnames(scores)[1] <- paste0(case, collapse = "")
英文:

There are two issues with your code.

First, + isn’t overloaded for string concatenation in R as it is in Python. Instead, use paste() (default separator = " ") or paste0() (separator = ""). e.g., paste0(case[[i]], case[[i+1]]).

Second, your for loop doesn’t make sense, and isn’t needed if I understand your goal. As written, it overwrites the first column name on each iteration, and on the last iteration, case[[i + 1]] tries to access an index one greater than the total length of the vector, which by definition doesn’t exist.

Using paste0() and ditching the for loop:

if (length(case) > 1) {
  colnames(scores)[1] <- paste0(case[[1]], case[[2]])
} else {
  colnames(scores)[1] <- case
}

head(scores)

      CRPD Gender treatment Cancer_Type CD4-T-cells
Pt1     PD   male  anti-PD1    Melanoma -0.07410987
Pt10    SD female  anti-PD1    Melanoma -0.09440127
Pt101   PR female  anti-PD1    Melanoma  0.04102849
Pt103   PD female  anti-PD1    Melanoma -0.16330295
Pt106   PD   male  anti-PD1    Melanoma -0.09424782
Pt11    PD female  anti-PD1    Melanoma -0.16731441

If your goal is to concatenate all elements of case regardless of length, you can get rid of the if statement too and just do:

colnames(scores)[1] <- paste0(case, collapse = "")

huangapple
  • 本文由 发表于 2023年1月8日 23:55:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定