我有一个如下的df,它有5个家庭的20个人.家庭中的一些人缺少他们是否有医疗卡的数据.我想给这些人提供与其家庭中其他人相同的价值(不是NA值,实际二进制值是0或1).
我尝试了以下代码,这是我认为正确方向的一步 - 但不是100%正确,因为a)如果每个家庭的med_card的第一个值是NA而b)它没有,它就不起作用为所有家庭成员取代NA 1.
DF<- ddply(df, .(hhold_no), function(df) {df$med_card[is.na(df$med_card)] <- head(df$med_card, na.rm=TRUE); return(df)})
Run Code Online (Sandbox Code Playgroud)
任何指针都将非常感谢,谢谢
样本df
df
person_id hhold_no med_card
1 1 1 1
2 2 1 1
3 3 1 NA
4 4 1 NA
5 5 1 NA
6 6 2 0
7 7 2 0
8 8 2 0
9 9 2 0
10 10 3 NA
11 11 3 NA
12 12 3 NA
13 13 3 1
14 14 3 1
15 15 4 1 …Run Code Online (Sandbox Code Playgroud) 我有一个df,我想删除df中行数少于X的人.例如,在这个玩具示例中,我想留下那些> = 5行的人.
df
names fruit
4 john kiwi
7 john apple
9 john banana
13 john orange
14 john apple
2 mary orange
5 mary apple
8 mary orange
10 mary apple
12 mary apple
1 tom apple
3 tom banana
6 tom apple
11 tom kiwi
Run Code Online (Sandbox Code Playgroud)
示例输出
df
names fruit
4 john kiwi
7 john apple
9 john banana
13 john orange
14 john apple
2 mary orange
5 mary apple
8 mary orange
10 mary apple
12 …Run Code Online (Sandbox Code Playgroud) 我有一个名字和一些资格日期的df.我想根据时间创建一个人有多少独特elig_end_dates的指标.这是我的df:
names date_of_claim elig_end_date
1 tom 2010-01-01 2010-07-01
2 tom 2010-05-04 2010-07-01
3 tom 2010-06-01 2014-01-01
4 tom 2010-10-10 2014-01-01
5 mary 2010-03-01 2014-06-14
6 mary 2010-05-01 2014-06-14
7 mary 2010-08-01 2014-06-14
8 mary 2010-11-01 2014-06-14
9 mary 2011-01-01 2014-06-14
10 john 2010-03-27 2011-03-01
11 john 2010-07-01 2011-03-01
12 john 2010-11-01 2011-03-01
13 john 2011-02-01 2011-03-01
Run Code Online (Sandbox Code Playgroud)
这是我想要的输出:
names date_of_claim elig_end_date obs
1 tom 2010-01-01 2010-07-01 1
2 tom 2010-05-04 2010-07-01 1
3 tom 2010-06-01 2014-01-01 2
4 tom 2010-10-10 2014-01-01 …Run Code Online (Sandbox Code Playgroud) 我的问题是提高代码的效率/优雅度。我有一个药品清单。我想确定以C09和C10开头的药物。如果一个人有这些药物,我想给他们一个二元指标(1 =是,0 =否),以表明他们是否有这些药物。二进制指示器将在同一数据框中的新列“ statins”中。我以这篇文章作为指导:SQL的LIKE'description%'语句的R等效项是什么?。
这是我所做的;
names<-c("tom", "mary", "mary", "john", "tom", "john", "mary", "tom", "mary", "tom", "john")
drugs<-c("C10AA05", "C09AA03", "C10AA07", "A02BC01", "C10AA05", "C09AA03", "A02BC01", "C10AA05", "C10AA07", "C07AB03", "N02AA01")
df<-data.frame(names, drugs)
df
names drugs
1 tom C10AA05
2 mary C09AA03
3 mary C10AA07
4 john A02BC01
5 tom C10AA05
6 john C09AA03
7 mary A02BC01
8 tom C10AA05
9 mary C10AA07
10 tom C07AB03
11 john N02AA01
ptn = '^C10.*?'
get_statin = grep(ptn, df$drugs, perl=T)
stats<-df[get_statin,]
names drugs
1 tom …Run Code Online (Sandbox Code Playgroud) 我有一个简单的查询 - 并且已经搜索过,因为我认为它可能在之前出现过,但是没有找到合适的答案.所以这里:
我有如下df
names drugs dates olds
4 john A02BC01 2010-05-01 0
6 john C09AA03 2010-08-01 0
11 john N02AA01 2010-06-14 0
2 mary C09AA03 2010-06-01 0
3 mary C10AA07 2010-07-01 0
7 mary A02BC01 2010-07-01 0
9 mary C10AA07 2010-07-24 0
1 tom C10AA05 2010-04-06 0
5 tom C10AA05 2009-12-01 1
8 tom C10AA05 2010-08-01 0
10 tom C07AB03 2010-05-12 0
Run Code Online (Sandbox Code Playgroud)
汤姆有一个二元指标.因为他有这个,我想删除所有汤姆的行.
期望的输出是
names drugs dates olds
4 john A02BC01 2010-05-01 0
6 john C09AA03 2010-08-01 0
11 …Run Code Online (Sandbox Code Playgroud) 我的df如下:
data
names fruit
7 john apple
13 john orange
14 john apple
2 mary orange
5 mary apple
8 mary orange
10 mary apple
12 mary apple
1 tom apple
6 tom apple
Run Code Online (Sandbox Code Playgroud)
我想做两件事.首先,计算具有苹果和橙色(即2玛丽和约翰)的独特观察的数量.
之后,我想将它们从我的数据框中删除,这样我就只剩下只有苹果的独特个体.
这就是我尝试过的
toremove<-unique(data[data$fruit=='apple' & data$fruit=='orange',"names"]) ##this part doesn't work, if it had I would have used the below code to remove the names identified
data2<-data[!data$names %in% toremove,]
Run Code Online (Sandbox Code Playgroud)
真的,我想使用grepl,因为我的真实数据比水果更复杂.这是我尝试过的(首先转换为data.table)
data1<-data.table(data1)
z<-data1[,ind := grepl('app.*? & orang.*?', fruit), by='names'] ## this works fine when i just use 'app.*?' …Run Code Online (Sandbox Code Playgroud)