-2

I have a dataframe statistics and a logic variable fromWidget, and for the variable from fromWidget who have 'TRUE' I want to replace statistics$app_space with 'Widget'. fromWidget and statistics are part of another dataframe BPFjfiles.

fromWidget [1] TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE

statistics$app_space [1] "www" "www" "www" "www" "www" "www" "www" "www" "www" "www"

I tried looping trough BPFjfiles, but this didn't work.

Orderplatforms <- lapply(BPFjfiles, function(x) {
  x$statistics$app_space["TRUE" %in% x$fromWidget] <- "Widget"
  x
  Orderplatforms <- x$statistics$app_space
})
handep
  • 21
  • 6
  • Please make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by adding sample data and expected output. – Ronak Shah Jul 11 '19 at 09:28

1 Answers1

0

The condition "TRUE" %in% x$fromWidget will always be true if at least one element from x$fromWidgetis true.

To replace x$app_space with "Widget" only when x$fromWidget is True you can do :

x$statistics$app_space[x$fromWidget] <- "Widget"
fmarm
  • 4,209
  • 1
  • 17
  • 29
  • I also got the following error _Error in `*tmp*`$statistics : $ operator is invalid for atomic vectors_ . Statistics is a dataframe so I don't understand this. Thanks for your help – handep Jul 11 '19 at 09:23
  • Can you please post an example of `x` so I can test it on my PC ? – fmarm Jul 11 '19 at 09:34
  • `x` is used in `function(x)` so it is the same structure of `BPFjfiles` – handep Jul 11 '19 at 09:48
  • Ok, so can you give an example of `BPFjfiles`? – fmarm Jul 11 '19 at 09:51
  • `BPjfiles` is a dataframe which exists out of a dataframe `statistics` and a logical variable `fromWidget` `fromWidget [1] TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE` `statistics$app_space [1] "www" "www" "www" "www" "www" "www" "www" "www" "www" "www` `statistics$client_id[1] NA NA NA NA NA NA NA NA NA NA` – handep Jul 11 '19 at 09:53
  • Ok, so I think the problem is that in your function, `fromWidget` is not in `x`, can you try `x$statistics$app_space[fromWidget] <- "Widget"` ? – fmarm Jul 11 '19 at 10:01
  • `Error in x$statistics$app_space[fromWidget] <- "Widget" : object 'fromWidget' not found` – handep Jul 11 '19 at 10:11
  • Can you post the result of `str(BPFjfiles)` ? – fmarm Jul 11 '19 at 10:51
  • It's quite long, where would be the best place to post it? – handep Jul 11 '19 at 13:00
  • You can add at the end of your question – fmarm Jul 11 '19 at 13:12