I am a very novice in R programing tidyverse.
As you can see below, I want to fill the NAs corresponding to the Item name of each row and column.
And then I want to leave only one row per a district name.
How could I do this in tidyverse or just typical R programing?
from
District Name Item Score TB_E04a TB_E04b TB_E04d
------------------------------------------------------------
Anderson County TB_E04a 0.51 NA NA NA
Anderson County TB_E04b 0.68 NA NA NA
Anderson County TB_E04d 0.67 NA NA NA
Clinton City TB_E04a 1.00 NA NA NA
Clinton City TB_E04b 1.00 NA NA NA
Clinton City TB_E04d 1.00 NA NA NA
to
District Name Item Score TB_E04a TB_E04b TB_E04d
------------------------------------------------------------
Anderson County TB_E04a 0.51 0.51 0.68 0.67
......(So on)
CodePudding user response:
Something like this?
library(dplyr)
library(tidyr)
df %>%
select(-starts_with("TB")) %>%
pivot_wider(
names_from = Item,
values_from = Score
)
District Name TB_E04a TB_E04b TB_E04d
<chr> <chr> <dbl> <dbl> <dbl>
1 Anderson County 0.51 0.68 0.67
2 Clinton City 1 1 1
Base R: with reshape
df1 <- reshape(df,
v.names = "Score",
idvar = "District",
timevar = "Item",
direction = "wide")
df1 <- df1[,c(1:2, 6:8)]
df1
District Name Score.TB_E04a Score.TB_E04b Score.TB_E04d
1 Anderson County 0.51 0.68 0.67
4 Clinton City 1.00 1.00 1.00