数据框
year1 year2
2009 2002
2003 2010
2005 2005
2006 2005
2005 NA
NA 2006
我希望通过比较两列来使第三列的值较小。 所以输出就像
year1 year2 year
2009 2002 2002
2003 2010 2003
2005 2005 2005
2006 2005 2005
2005 NA 2005
NA 2006 2006
对于我的方法,我将NA更改为1 adn,然后比较两列,但是如果我使用,它只会给我更高的价值或错误的价值,就像在另一边一样: 小于那么只会在不正确的列中给我1。
startdate$year<-ifelse(as.numeric(startdate$year1)==as.numeric(startdate$year2),
startdate$year1,startdate$year)
startdate$year<-ifelse(as.numeric(startdate$year1)<as.numeric(startdate$year2),
startdate$year2,startdate$year)
startdate$year<-ifelse(as.numeric(startdate$year1)>as.numeric(startdate$year2),
startdate$year1,startdate$year)
预先感谢:)
答案 0 :(得分:4)
您可以使用pmin
df$year = pmin(df$year1, df$year2, na.rm=TRUE)
df
year1 year2 year
1 2009 2002 2002
2 2003 2010 2003
3 2005 2005 2005
4 2006 2005 2005
5 2005 NA 2005
6 NA 2006 2006
答案 1 :(得分:2)
另一种选择是将apply
与min
一起使用。
dat$year <- apply(dat, 1, min, na.rm = TRUE)
dat
# year1 year2 year
# 1 2009 2002 2002
# 2 2003 2010 2003
# 3 2005 2005 2005
# 4 2006 2005 2005
# 5 2005 NA 2005
# 6 NA 2006 2006
数据
dat <- read.table(text = "year1 year2
2009 2002
2003 2010
2005 2005
2006 2005
2005 NA
NA 2006",
header = TRUE)
答案 2 :(得分:1)
这里是带有pmap
library(tidyverse)
dat %>%
mutate(year = pmap(., min, na.rm = TRUE))
# year1 year2 year
#1 2009 2002 2002
#2 2003 2010 2003
#3 2005 2005 2005
#4 2006 2005 2005
#5 2005 NA 2005
#6 NA 2006 2006
或者使用pmin
并且如果有很多列,
dat %>%
mutate(year = pmin(!!! rlang::syms(names(.)), na.rm = TRUE))