我正尝试在我的数据框中创建一个新列,基于另一个数据框中的字符串子集。这是我的数据框
df =structure(list(Combination = c("BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite",
"BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite",
"BRUV_Acoustic_Satellite", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Acoustic", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Stationary_Radio", "Stationary_Radio",
"Stationary_Radio", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival",
"Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite",
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival",
"Stationary_Archival", "Stationary_Acoustic_Radio_PIT", "Stationary_Acoustic_Radio_PIT",
"Stationary_Acoustic_Radio_PIT", "Controlled_Acoustic", "Controlled_Acoustic",
"Stationary_PIT", "Stationary_PIT", "Stationary_Acousitc_PIT",
"Stationary_Acousitc_PIT", "Stationary_Acousitc_PIT", "BRUV_Acoustic",
"BRUV_Acoustic", "BRUV_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Archival",
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival",
"Stationary_Satellite", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Acoustic", "Controlled_Acoustic", "BRUV_Acoustic",
"BRUV_Acoustic", "BRUV_Acoustic", "Animalborne_Satellite", "Animalborne_Satellite",
"Stationary_Archival", "Stationary_Archival", "Stationary_Archival",
"Stationary_Radio_PIT", "Stationary_Radio_PIT", "Controlled_Acoustic",
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Satellite", "Controlled_Satellite", "Controlled_Satellite",
"Controlled_Satellite", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival_PIT", "Animalborne_Archival_PIT", "Animalborne_Archival_PIT",
"Animalborne_Acoustic_Archival", "Animalborne_Acoustic_Archival",
"Animalborne_Acoustic_Archival", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Animalborne_Acoustic", "Animalborne_Acoustic", "Animalborne_Acoustic",
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_PIT",
"Stationary_Acoustic_PIT", "Stationary_Acoustic_PIT", "BRUV_Acoustic",
"BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic",
"BRUV_Acoustic", "Controlled_Archival", "Controlled_Archival",
"Controlled_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Radio",
"Stationary_Acoustic_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_Archival", "Controlled_Acoustic", "Controlled_Acoustic",
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic",
"Stationary_Acoustic", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival",
"Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Stationary_Satellite", "Stationary_Satellite",
"Stationary_Satellite", "Stationary_Satellite", "Stationary_Satellite",
"Animalborne_Archival", "Animalborne_Archival", "Stationary_Acoustic_Radio",
"Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Stationary_Acoustic", "Stationary_Acoustic", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "Animalborne_Archival", "Animalborne_Archival",
"Animalborne_Archival", "BRUV_Acoustic", "BRUV_Acoustic", "BRUV_Acoustic",
"BRUV_Acoustic", "BRUV_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Animalborne_Satellite_Archival", "Animalborne_Satellite_Archival",
"Animalborne_Satellite_Archival", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Acoustic")), class = "data.frame", row.names = c(NA,
-245L))
我需要一个新列,其中的值与一些字符串相关,因此对于每个具有Acoustic
、radio
或者PIT
,新列中的值应该是receiver based
,其他值应该是non receiver based
,但是对于包含acoustic
和satellite
的列,我需要新列中的值是Both
。
我已尝试使用以下代码执行ifelse
命令
df$Type = ifelse(df$Combination == "Acoustic", 'Non Receiver Based', 'Receiver Based')
但是它把它们都叫做receiver based
,我不知道如何把我上面提到的所有论点结合起来。
5条答案
按热度按时间yh2wf1be1#
可以使用
grepl()
检查字符串中是否出现某种模式,然后使用if-else语句确定大小写。由于if()
没有矢量化,因此需要将其 Package 在Vectorize()
中,以便在mutate()
中使用。sq1bmfud2#
使用
grepl
检查字符串中是否出现了其中一个单词。模式由以|
分隔的单词组成,即检查字符串中是否出现了以下单词之一:dplyr
的另一种选择是使用case_when
,这可能更容易理解:zpf6vheq3#
您可以使用
tidyverse
中的一些函数。下面,我创建了一个名为new_col
的新列,它是您想要的输出:zf9nrax14#
像这样的东西对你有帮助吗?
这是一个相当肮脏的解决方案,我相信有人会找到一个更好的,只有工作,如果声学_卫星是一个单一的字在您的数据集,但它做的工作。
js5cn81o5#
一个简单的逐步方法可以是:
只是为了好玩的基准:
结果