简体   繁体   中英

Data spreading and simultaneous NA dropping in R?

I need to reshape a data frame in R, and I couldn't find a post describing a solution for the type of reshaping that I need. My data frame looks like this:

# Sample data frame.
time        <- c(1:8)
aoi         <- c("a", "b", "c", NA, "a", "c", NA, "b")
df          <- data.frame(time, aoi)

time aoi
1    a
2    b
3    c
4 <NA>
5    a
6    c
7 <NA>
8    b

I would like to take each value in the column aoi and make it a separate column, such that a row receives a 1 if that particular value was present, and 0 otherwise. Importantly, there are NAs in the data frame so I would like them to be dropped in the ourput. I have tried different functions in dplyr and tidyr (eg spread(aoi, -time) ), but I can't use 0s and 1s for the replacements and I am also having difficulty dropping the NAs and writing 0S in their place in the other columns. How would you do it? This is how my target output should look like:

time a b c
1    1 0 0
2    0 1 0
3    0 0 1
4    0 0 0
5    1 0 0
6    0 0 1
7    0 0 0
8    0 1 0

Thanks for your help!

Did you try:

df1 <- spread(df,aoi,-time)
data.frame(time=d1$time
 ,a=as.integer(!is.na(df1$a))
 ,b=as.integer(!is.na(df1$b))
 ,c=as.integer(!is.na(df1$c)))

#  time a b c
#1    1 1 0 0
#2    2 0 1 0
#3    3 0 0 1
#4    4 0 0 0
#5    5 1 0 0
#6    6 0 0 1
#7    7 0 0 0
#8    8 0 1 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM