Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing columns of data frames in lists in R
    text
    copied!<p>I want to do some statistic analysis in R on a column with the same name but different length originating from several data frames. I created a list:</p> <pre><code>my.list &lt;- list(df1, df2, df3, df4) </code></pre> <p>Now, as some elements of the column of interest (say: my.col) contain the word "FAILED" instead of numbers, I replace it by 'NA':</p> <pre><code>for (i in 1:length(my.list)){ for (j in 1:length(my.list[[i]]$my.col)){ if (my.list[[i]]$my.col[j] %in% c("FAILED")) {my.list[[i]]$my.col[j] &lt;- 'NA'}; } } </code></pre> <p>I am pretty sure that this is not the best solution for the problem, but at least it works. Although I have to say that it causes warnings that in another column (not my.col) there are unvalid factor levels that have been replaced by 'NA'. No idea why it actually considers other columns than my.col. Suggestions for improvement are highly appreciated.</p> <p>Now, the remaining numbers contain decimal comma instead of point. Although I tried to eliminate this problems while importing the .csv-file with "dec=","", this does not work out for columns that contain anything else than numbers (e.g. "FAILED"). So I have to replace the comma by the point, and this is what doesn't work for me. I tried:</p> <pre><code>for (i in 1:length(my.list)){ as.numeric(gsub(",", ".", my.list[[i]]$my.col)) } </code></pre> <p>This doesn't give any errors, but it also doesn't change anything, although if I type in e.g.</p> <pre><code>as.numeric(gsub(",", ".", my.list[[4]]$my.col)) </code></pre> <p>it does what I want to do for the 4th element of the list. From my point of view, both should be the same. What's the problem with this?</p> <p>Btw, I prefer not to delete the other columns from the data frames because I might need them in future for other analysis.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload