0

I do have a directed network with a lot of NA's for a couple of "targets". The original dataframe "df2_net" looks like...

from      to    Attribute
<int>    <int>    <int>
 5       NA    12120
 6       NA    12210
 3       NA    12300
 6       NA    12310
 5       6     12310
 3       5     12310

if I generate the network in a straight forward way:

g <- graph.data.frame(df2_net, directed = T)

I get the error: In graph.data.frame(df2_net, directed = T) : In d' NA' elements were replaced with string "NA"

I don't want to just clean things up (df2_net_clean <- na.omit(df2_net))

I would rather want to have the NA's as a kind of self-loop for the "from" variables.

PeteWoods
  • 37
  • 2
  • 2
    Why don't you just overwrite the NA value with that in the `from` column? `df2_net[is.na(df2_net$to)]$to <- df2_net$from` ? – Merik Jul 23 '18 at 20:33
  • Thanks for your suggestion! Now I do get the error: [Error in <-.data.frame`(`*tmp*`, is.na(df2_net$to), value = c(5L, : duplicate subscripts for columns] – PeteWoods Jul 23 '18 at 20:37
  • I didn't test my code before suggesting it, and just wanted to give you a general direction. It seems like @divibisan has taken the extra step of making it a working piece of code :) – Merik Jul 23 '18 at 20:44

1 Answers1

1

With credit to @Merik for the suggestion, you can just replace the values of to with the value of from for all rows where to is NA:

df2_net[is.na(df2_net $to), 'to'] <- df2_net[is.na(df2_net$to), 'from']
divibisan
  • 11,659
  • 11
  • 40
  • 58