English 中文(简体)
符合某种条件的缝 cells室,但根据已经填满的囚室确定条件
原标题:Fill cells that match a certain condition, but the condition is defined based on already filled rows

I have the following data set:

  hs desc
1 34    a
2 53    b
3 64    c
4 54    d
5 34 <NA>
6 34 <NA>
7 53 <NA>
8 53    b
9 53    b
10 53 <NA> 

首先,在一个理想的世界里,每小时休息都具有一定价值,因此, des应该具有另一个价值。 例如,每看34小时,就请看“a” des。 但是,正如你从我的数据集中可以看到的那样,我国有许多国民。 我需要用相应的价值观填写这些单元。

It may appear simple from the start. I could use ifelse or case_when. But that s not enough, because my real database has A LOT of different hs codes

我需要这样做:

  1. It should see the filled cells and understand that everytime a hs has a certain value, desc should have a corresponding value
  2. But the code should do this based on the already filled values. I m not telling my code explicitly that if hs=34, then desc=a. He knows it by itself after scanning my dataset
  3. Once the code itself knows what desc should be based on hs, then it should fill all NAs apropriately

例如:

  1. The code scans my dataset and sees, in any already filled row, that if hs=34, then desc=a.
  2. After that, the code should put desc=a everytime he sees hs=34

我知道这令人困惑,但我希望有人理解我。

问题回答

您可以首先使用<代码>filter ,将其原始数据框架排除在外,以排除NA,这将是你的“参考”数据框架。 <代码>left_join> 填写NA的本参考数据框架的原文。

library(dplyr)

left_join(
  df |> select(hs),
  df |> 
    filter(!is.na(desc)) |> 
    distinct(),
  by = "hs"
)

   hs desc
1  34    a
2  53    b
3  64    c
4  54    d
5  34    a
6  34    a
7  53    b
8  53    b
9  53    b
10 53    b

You can use fill() from tidyr by groups.

library(tidyverse)

df %>%
  group_by(hs) %>%
  fill(desc, .direction = "downup") %>%
  ungroup()

# # A tibble: 10 × 2
#       hs desc 
#    <int> <chr>
#  1    34 a    
#  2    53 b    
#  3    64 c    
#  4    54 d    
#  5    34 a    
#  6    34 a    
#  7    53 b    
#  8    53 b    
#  9    53 b    
# 10    53 b

Data

df <- read.table(text = "  hs desc
1 34    a
2 53    b
3 64    c
4 54    d
5 34 <NA>
6 34 <NA>
7 53 <NA>
8 53    b
9 53    b
10 53 <NA> ", na.strings = "<NA>")




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签