我有一个数据框
data <- data.frame('a' = c('A','B','C','D','E'),
'x' = c(1,2,NA,NA,NA),
'y' = c(NA,NA,3,NA,NA),
'z' = c(NA,NA,NA,4,NA))
看起来像这样:
a x y z
1 A 1 NA NA
2 B 2 NA NA
3 C NA 3 NA
4 D NA NA 4
5 E NA NA NA
我希望得到这样的数据:
a N
1 A 1
2 B 2
3 C 3
4 D 4
5 E NA
谢谢!
答案 0 :(得分:4)
使用coalesce
的 dplyr 解决方案。
library(dplyr)
data %>%
mutate(N = coalesce(x, y, z)) %>%
select(a, N)
a N
1 A 1
2 B 2
3 C 3
4 D 4
5 E NA
select
不需要transmute
:
data %>%
transmute(a, N = coalesce(x, y, z))
答案 1 :(得分:1)
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
NavigatorIOS,
View
} from 'react-native';
class HelloWorld extends Component {
答案 2 :(得分:1)
pmax
似乎在这里提出了建议,与循环每一行相比,大数据应该快得多:
do.call(pmax, c(data[c("x","y","z")],na.rm=TRUE) )
#[1] 1 2 3 4 NA
cbind(data["a"], N=do.call(pmax, c(data[c("x","y","z")],na.rm=TRUE) ))
# a N
#1 A 1
#2 B 2
#3 C 3
#4 D 4
#5 E NA