我是R的新手,需要按相似性对行进行分组和填充。 我有一个如下所示的数据框:
Name Job Gender City
California NA NA 1
Bob plumber M 0
Carol nurse F 0
Chicago NA NA 1
Tom Chef M 0
Ann Swimmer F 0
Joy Police F 0
我需要数据框看起来像这样:
Name Job Gender City
Bob plumber M California
Carol nurse F California
Tom Chef M Chicago
Ann Swimmer F Chicago
Joy Police F Chicago
道歉,如果这与另一个问题相似 - 再次,我对此很新。谢谢!
答案 0 :(得分:1)
假设组开始于@ApplicationPath("service")
public class AdminApplication extends Application {
}
等于1,并使用City
(或NA
)列中的Job
值,我们可以执行以下操作。
Gender
数据:
na.omit(transform(df, City = Name[is.na(Job)][cumsum(City)]))
# Name Job Gender City
# 2 Bob plumber M California
# 3 Carol nurse F California
# 5 Tom Chef M Chicago
# 6 Ann Swimmer F Chicago
# 7 Joy Police F Chicago
答案 1 :(得分:0)
使用zoo
,我分解了步骤
library(zoo)
dat1$City[dat1$City==1]=dat1$Name[dat1$City==1]
dat1$City[dat1$City==0]=NA
dat1$City=na.locf(dat1$City)
dat1=dat1[!is.na(dat1$Gender),]
dat1
Name Job Gender City
2 Bob plumber M California
3 Carol nurse F California
5 Tom Chef M Chicago
6 Ann Swimmer F Chicago
7 Joy Police F Chicago
答案 2 :(得分:0)
另一种方法是使用fill
包中的tidyr
。
我添加了一个新变量City_Name
并保留了原始City
变量(因为OP的评论表明他们可能仍希望在结果中看到该信息)。
library(dplyr)
library(tidyr)
df %>%
mutate(City_Name = if_else(City == 1, Name, NA_character_)) %>%
fill(City_Name) %>%
filter(City == 0)
#> Name Job Gender City City_Name
#> 1 Bob plumber M 0 California
#> 2 Carol nurse F 0 California
#> 3 Tom Chef M 0 Chicago
#> 4 Ann Swimmer F 0 Chicago
#> 5 Joy Police F 0 Chicago
数据强>
df <- read.table(text = "Name Job Gender City
California NA NA 1
Bob plumber M 0
Carol nurse F 0
Chicago NA NA 1
Tom Chef M 0
Ann Swimmer F 0
Joy Police F 0", header = TRUE, stringsAsFactors = FALSE)