我有一组列,我需要一个新列min123
,其中列123a_1
到123a_5
的最小值。
dff <- structure(list(`MCI ID` = c("070405344", "230349820", "260386435","370390587", "380406805", "391169282", "440377986", "750391394","890373764", "910367024"),
`123a_1` = structure(c(16672, 16372,16730, 16688, 16700, 16783, 16709, 17033, 16786, 16675), class = "Date"),
`123a_2` = structure(c(17029, 16422, 17088, 17036, 17057,17140, 17072, 17043, 17141, 17038), class = "Date"),
`123a_3` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date"),
`123a_4` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date"),
`123a_5` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date")),
.Names = c("MCI ID","123a_1", "123a_2", "123a_3", "123a_4", "123a_5"), row.names = c(NA,10L), class = "data.frame")
答案 0 :(得分:3)
使用do.call
和pmin
的基础R方法:
dff$min123 <- do.call(pmin, c(dff[-1], na.rm = TRUE))
dplyr
中的类似appraoch:
library(dplyr)
dff %>%
mutate(min123 = do.call(pmin, c(select(., -1), na.rm = TRUE)))
或data.table
:
library(data.table)
setDT(dff)[, min123 := do.call(pmin, c(.SD, na.rm = TRUE)), .SDcols = -1]
答案 1 :(得分:2)
library(dplyr)
dff %>%
mutate(min123 = pmin(`123a_1`, `123a_2`, `123a_3`, `123a_4`, `123a_5`, na.rm = T))
答案 2 :(得分:0)
这就是函数$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input="Billy got score of 2 and Tommy got score of 3"
>>> import re
>>> re.split(r'score of \d+(?:\s*and\s*)?',input)
['Billy got ', 'Tommy got ', '']
的用途:
pmin