英文:
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
或类似的东西。
如果我尝试上面的代码,我会得到以下错误,当然这是因为CR
和PD
是字符串,这里不会像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 = "")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论