英文:
Adding rows depending on condition
问题
我需要帮忙解决一个非常简单的问题:
假设我有这个数据框:
data_new <- data.frame(section = c("1", "4", "5", "6"),
density = c("0.2", "0.7", "0.8", "0.2"))
> data_new
section density
1 1 0.2
2 4 0.7
3 5 0.8
4 6 0.2
我需要添加行,因为整个表是基于6个部分,但只有4个部分的数据。这意味着在这种情况下,我需要添加2行(部分2和3),密度为0,所以我有:
> data_desired
section density
1 1 0.2
2 4 0.7
3 5 0.8
4 6 0.2
5 2 0
6 3 0
关键是*0密度*行的组合可能会变化。在这种情况下,部分3和4是空的,但下一次可能没有部分具有0密度,或者我需要添加5个部分等等。这可能会有很大的变化,从一个部分具有数据到所有部分具有数据。
我确信有一种优雅的方法可以添加到我的管道中以添加我需要的行,这是特定情况下的。非常感谢您的帮助!
英文:
I need a little help with a very simple question:
Let's say I have this data frame:
data_new <- data.frame(section = c("1", "4", "5","6"),
density = c("0.2", "0.7", "0.8", "0.2"))
> data_new
section density
1 1 0.2
2 4 0.7
3 5 0.8
4 6 0.2
I need to add rows because the full table is based on 6 sections, but only have data on 4. This means that in this case I have to add 2 rows (sections 2 and 3) with density 0 so I have:
> data_desired
section density
1 1 0.2
2 4 0.7
3 5 0.8
4 6 0.2
5 2 0
6 3 0
The point is that the combination of 0 density rows may vary. In this case sections 3 and 4 were empty, but next time it may be that no section has density 0 or that I have to add 5 sections, etc. It can vary a lot, from 1 section with data to all sections with data.
I'm sure there is an elegant way to add to my pipe to ad the rows I need and that is case specific. Thanks a lot for your help!!
答案1
得分: 1
使用 rows_update
的另一个选项:
library(dplyr)
# 创建零密度的数据框
n <- 6
data_zero <- data.frame(section = as.character(c(1:n)),
density = as.character(rep(0,n)))
data_new <- data.frame(section = c("1", "4", "5", "6"),
density = c("0.2", "0.7", "0.8", "0.2"))
rows_update(data_zero, data_new)
section density
1 1 0.2
2 2 0
3 3 0
4 4 0.7
5 5 0.8
6 6 0.2
对于多列数据:
library(dplyr)
n <- 6
data_zero <- data.frame(section = as.character(c(1:n)),
density = as.character(rep(0,n)))
data_new <- data.frame(section = c("1", "4", "5", "6"), density = c("0.2", "0.7", "0.8", "0.2"), potatoes = c("a", "n", "ed", "3"))
rows_update(data_zero, data_new[,c('section','density')]) %>%
merge(data_new, all.x = TRUE)
section density potatoes
1 1 0.2 a
2 2 0 <NA>
3 3 0 <NA>
4 4 0.7 n
5 5 0.8 ed
6 6 0.2 3
英文:
Another option using rows_update
:
library(dplyr)
#create zero density dataframe
n<-6
data_zero <- data.frame(section = as.character(c(1:n)),
density = as.character(rep(0,n)))
data_new <- data.frame(section = c("1", "4", "5","6"),
density = c("0.2", "0.7", "0.8", "0.2"))
rows_update(data_zero ,data_new)
section density
1 1 0.2
2 2 0
3 3 0
4 4 0.7
5 5 0.8
6 6 0.2
For multiple columns:
library(dplyr)
n<-6
data_zero <- data.frame(section = as.character(c(1:n)),
density = as.character(rep(0,n)))
data_new <- data.frame(section = c("1", "4", "5","6"), density = c("0.2", "0.7", "0.8", "0.2"), potatoes = c("a","n","ed","3"))
rows_update(data_zero ,data_new[,c('section','density')]) %>%
merge(data_new,all.x=T)
section density potatoes
1 1 0.2 a
2 2 0 <NA>
3 3 0 <NA>
4 4 0.7 n
5 5 0.8 ed
6 6 0.2 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论