-3

i'm trying to use this R script, but the problem is that it's not working the way i want.

Here is the script:

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)

df = read.table(args[1], header=FALSE, sep="\t", row.names=1)
#print(df)

df.0 <- apply(df, c(1,2), function(x) {if(x>0) return (x) }) #this function doesn't work it must select only data >0
df.0

the input data is like this:

mir1306 0
mir151  0
mir302b 0
mir5124 0
mir744  0
mmu-let-7a-5p   60
mmu-let-7b-5p   15
mmu-let-7c-5p   77
mmu-let-7f-5p   60
mmu-let-7i-5p   22

and i get this error :

                V2  
mir1306         NULL
mir151          NULL
mir302b         NULL
mir5124         NULL
mir744          NULL
mmu-let-7a-5p   60  
mmu-let-7b-5p   15  
mmu-let-7c-5p   77  
mmu-let-7f-5p   60  
mmu-let-7i-5p   22 

i don't want of the lines containing value == 0, i only want to keep data with value > 0

I tried other solution but nothing worked...

Thanks you !

edit:

i tried this way :

df.0 <- df[, df$V2>0]

but i get this error message :

Error in `[.data.frame`(df, , df$V2 > 0) : undefined columns selected
Calls: [ -> [.data.frame
D Prat
  • 352
  • 4
  • 16
  • 1
    So you want to subset data? How is this related to [tag:pie-chart]? – pogibas Jun 15 '18 at 09:28
  • you're right i deleted the pie chart tag sorry for that, but yes indeed i would like to make a subset – D Prat Jun 15 '18 at 09:30
  • You question might be duplicate of this one [Subset dataframe by multiple logical conditions of rows to remove](https://stackoverflow.com/questions/6244217/subset-dataframe-by-multiple-logical-conditions-of-rows-to-remove) – pogibas Jun 15 '18 at 09:31
  • I think the confusion was not only about the pie chart tag, but is about the code snippet that includes something about a pie chart. Try to reduce your example to only what is relevant for your question. And state explicitly what outcome you want. – otwtm Jun 15 '18 at 09:36
  • @PoGibas it's not corresponding exactly to what i would like to since i would like to work on integers and that in this post they are working on str Though i guess it could work, but not the way i want, if i had negative value it would not work... – D Prat Jun 15 '18 at 09:44
  • @otwtm you're right i edited my post for less confusion – D Prat Jun 15 '18 at 09:45
  • you need to provide a `dput`of your dataset. how on earth could anyone know what's in your `commandArgs(trailingOnly=TRUE)`. The answer is a simple subset or `[`and of course you're going to get an error if you select by an undefined column -- don't do it! But we can't help you because we don't know your data structure -- you haven't told us. – lebatsnok Jun 15 '18 at 09:57
  • @lebatsnok i'm not sure to understand what ask when you say "we don't know your data structure". I've put an example of my data input : which is structured with line, for each line there 2 col : first one corresponding to a name and the second one to a value – D Prat Jun 15 '18 at 10:09
  • d prat you're reading your data from a file on your hard drive. we don't have your hard drive. we don't know what are the variable names in your file. I presumed you have `V2` among the names but that's just a guess. – lebatsnok Jun 15 '18 at 10:21

1 Answers1

1

to subset data you can use subset:

subset(df , V2 > 0)

presuming that df has a column named V2 and you want to subset based on this. Alternatively,

df[df$V2>0,]
lebatsnok
  • 6,329
  • 2
  • 21
  • 22