有条件地组合数据框中不同列的单词

时间:2018-12-07 13:00:11

标签: r dataframe if-statement grepl

我有一个包含3列的数据框,所有这些都是字符。我想使用此数据框创建公式,因此需要确保数据框的格式正确,以便可以将其粘贴到方程式中。

第一列包含统计函数(例如log10,exp)。第二个包含一个解释变量。第三列包含有关模型类型的信息,该信息应用于定义是否应应用以下功能。

我想编辑第二列,以便将第一列的字母放在字符串的开头。但我想在第二栏中加上括号。而且我只想将此功能应用于第三列中包含特定单词的某些模型。因此,一些示例数据:

Function <- c("exp","log10","exp")
Variable <- c("x","y","z")
Model_type <- c("Model_Yes","Model_Yes","Model_No")
Test <- data.frame(Function,Variable,Model_type)
Test[, ] <- lapply(Test[, ], as.character)

基于类似How to use chained ifelse and grepl?这样的问题,我认为我需要这样的东西:

Test$Variable <- ifelse((grepl("No", Test$Model_type)),
                    Test$Variable,
                    paste(Test$Function,Test$Variable))

但这不适用于'No'Model_types,也不会在原始Variable字符串两边放置方括号。这就是我想要的输出。

Test$Variable <- c("exp(x)","log10(y)","z")

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用`getLocaltions(){ document.addEventListener("deviceready", function() { var div = document.getElementById("map_canvas"); var map_pomp = plugin.google.maps.Map.getMap(div); map_pomp.one(plugin.google.maps.event.MAP_READY, function() { // POMPIERS document.getElementById("pompier").addEventListener('click',() => { const POMPIERS = {"lat": 48.8472725, "lng": 2.3841632999999547}; map_pomp.addCircle({ 'center': POMPIERS, 'radius': 300, 'strokeColor' : '#AA00FF', 'strokeWidth': 5, 'fillColor' : 'yellow' }, function(circlepomp) { setTimeout(function() { circlepomp.setRadius(2500); }, 1000); // circlepolice.remove(); }); var pomp = [ { position: {lng: 2.3841632999999547, lat: 48.8472725}, title: "Sapeurs Pompiers", icon: "img/pompier.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.405336000000034, lat: 48.8547079}, title: "Sapeurs Pompiers Charonne", icon: "img/pompier.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.386519000000021, lat: 48.8362749}, title: "Centre de Secours Nativité Brigade des Sapeurs Pompiers de Paris", icon: "img/pompier.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.3618501999999353, lat: 48.85571950000001}, title: "Sapeurs Pompiers - Centre de Secours de Sévigné", icon: "img/pompier.png", animation: plugin.google.maps.Animation.DROP } ]; // Add markers var baseArrayClass = new plugin.google.maps.BaseArrayClass(pomp); baseArrayClass.map(function(options, cb) { // The variable "options" contains each element of the pomp. // // The variable "cb" is a callback function of iteration. map_pomp.addMarker(options, cb); }, function(markers) { // Set a camera position that includes all markers. var bounds = []; pomp.forEach(function(POI) { bounds.push(POI.position); }); map_pomp.moveCamera({ target: bounds }, function() { // After camera moves open the last marker. markers[markers.length - 4].showInfoWindow(); }); }); }); }); // ------------------------------------------------------------------------------- var map_police = plugin.google.maps.Map.getMap(div); map_police.one(plugin.google.maps.event.MAP_READY, function() { // POLICE document.getElementById("police").addEventListener('click',() => { const POLICE = {"lat": 48.8510612, "lng": 2.3671649000000343}; map_police.addCircle({ 'center': POLICE, 'radius': 300, 'strokeColor' : '#AA00FF', 'strokeWidth': 5, 'fillColor' : '#ddd' }, function(circlepolice) { setTimeout(function() { circlepolice.setRadius(2500); }, 1000); // circlepolice.remove(); }); var police = [ { position: {lng: -6.838469 , lat: 34.023334 }, title: "Rabat", 'snippet' : "home", icon: "img/home.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.380863399999953, lat: 48.84426}, title: "Commissariat central de police ", 'snippet' : "Paris 12e arrondissement", icon: "img/police.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.3785209000000123, lat: 48.8543145}, title: "Préfecture de Police ", 'snippet' : " 11ème Arrondissement", icon: "img/police.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.3671649000000343, lat: 48.8510612}, title: "Commissariat de Police", 'snippet' : "4e arrondissement", icon: "img/police.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.348542199999997, lat: 48.8494763}, title: "Commissariat de Police ", 'snippet' : "5e arrondissement", icon: "img/police.png", animation: plugin.google.maps.Animation.DROP }, { position: {lng: 2.3558738000000403, lat: 48.83303009999999}, title: "Police Nationale", icon: "img/police.png", animation: plugin.google.maps.Animation.DROP } ]; // Add markers var baseArrayClass = new plugin.google.maps.BaseArrayClass(police); baseArrayClass.map(function(options, cb) { // The variable "options" contains each element of the data. // // The variable "cb" is a callback function of iteration. map_police.addMarker(options, cb); }, function(markers) { // Set a camera position that includes all markers. var bounds = []; police.forEach(function(POI) { bounds.push(POI.position); }); map_police.moveCamera({ target: bounds }, function() { // After camera moves open the last marker. markers[markers.length - 3].showInfoWindow(); }); }); }); }); }); }` 将变量粘贴在一起,并添加滞后和前括号

paste0

所以,最后的命令应该是

paste0(Test$Function, "(", Test$Variable, ")")   
#[1] "exp(x)"   "log10(y)" "exp(z)"