使用mutate创建新列,该变量与数据帧(dplyr)中每一行的一组指定列的内容有关

时间:2018-06-22 15:34:33

标签: r dplyr

使用mutate创建新列,这是数据框中每一行的一组指定列的内容的功能。

这似乎应该是一个简单的任务,但我一直在努力寻找正确的语法,例如:

df <- data.frame("annotations"=c("some","information","in","columns"),
           "X001"=c(124,435,324,123),
           "X002"=c(486,375,156,375)) 

df %>% mutate(median=median(select(.,starts_with("X"))))

因此,我获得了带有新列“ median”的原始数据帧,该列的所有行的中位数为每行以“ X”开头。我认为我可能需要在某个地方放置rowwise()

我正在尝试将其放入更大的dplyr管道中,因此我正在“ tidyverse”中寻找解决方案

2 个答案:

答案 0 :(得分:1)

您可以在switch characteristic.uuid { case CBUUID(string: "D973F2E2-B19E-11E2-9E96-0800200C9A66"): print("Caractéristique E2") print("Nombre d'éléments: ", characteristic.value?.count as Any) let firstValue = characteristic.value?.contains(170) print("Est dedans? ", firstValue!) let size = characteristic.value?.count // I tried to put a if statement to check if i get a transfer with 0 array into it, but it didn't work... let data = characteristic.value var byte2 = [UInt8](repeating: 0, count: size!) //conversion into String let strArrayToString = arrayToString(_DicToJSONString: byte2) print("array intermédiaire: ", strArrayToString) NotificationCenter.default.post(name:NSNotification.Name(rawValue: "Notify"), object: nil) stringResultat += strArrayToString stringResultat = stringResultat.replacingOccurrences(of: "][", with: "/") print("array concaténée: ", stringResultat) //-----------The goal is that the following lines are called once the transfer is over (i can make a function, the point is how to call it) ------- print("array finale concaténée: ", stringResultat) //enregistrement dans le core data (ECRASEMENT A CHAQUE FOIS ?) if CoreDataHandler.saveObject(latitude: Localisation.getLatitude(), longitude: Localisation.getLongitude(), payload: stringResultat, GUIDCapt: "308922AB-0533-45F6-A294-5775B4FA5CCB") { print("enregistrement a peut etre fonctionné ... ") CoreDataHandler.TEST() } else { print("problème enregistrement") } 列上pmap

X

或使用library(tidyverse) df %>% mutate(median = pmap_dbl(select(., starts_with("X")) , ~median(c(...))))

apply

答案 1 :(得分:0)

不包括使用dplyr的另一种方式

function Foo(){this.bar=42;}
Object.defineProperty(Foo.prototype, "baz", {
  get:function(){return 25;},
  set:function(val){},
  enumerable: true
});
var foo = new Foo();
for(var x in foo) console.log(x);