简体   繁体   中英

How do I apply an ifelse function to all cells in a data frame?

I am trying to apply an ifelse statement to all the cells in my data frame. I'm pretty sure I am overthinking this but would appreciate some help/guidance!

I have a dataframe of (slightly modified) percent cover of vegetation from a number of sites where the site names and the vegetation types are the row names and column names, respectively (ie. the data frame should only consist of numeric values):

           dwarf shrub equisetum  forb fungi graminoid lichen  moss shrub-forb tall shrub tree
site1            33.25         0 21.25   1.0     35.25   3.25 60.00       0.00      34.25 0.25
site2            30.25         0 15.00   0.0     25.75   7.50 62.25       1.50      26.75    0
site3            50.00         0 10.00   0.5     23.50   3.25 65.00       6.75      18.50    0
site4            46.00         0  7.75   0.0     32.75   2.25 33.75       4.50      11.25 0.75
site5            28.00         0 11.00   0.0     40.00   6.00 30.00       0.00      38.00    0
site6            40.25         0 10.50   0.0      5.75   6.25  7.25       3.25       8.75 1.25

I am trying to round the numbers to the nearest whole number such that the round() function is used when the value is greater than 1 and the ceiling() function is used when the value is less than 1.

Here is the code I have written to try do this:

new.df <- if(old.df > 1){
                  round(old.df, digits = 0)} else{
                  ceiling(old.df)
}

I have also tried without the ceiling function:

new.df <- if(old.df > 1){
                  round(old.df, digits = 0)} else{
                  old.df == 1
}

I have not been successful in applying the second half of the statement (ceiling()). I get this error:

Warning message:
In if (old.df > 1) { :
  the condition has length > 1 and only the first element will be used

Any assistance would be much appreciated, thank you!

You mentioned ifelse , I think it's straight-forward enough to apply this to each column using lapply . (I'll add the isnum check in case there are non-numeric columns in the data, feel free to ignore it if your data is always numeric .)

isnum <- sapply(dat, is.numeric)
dat[isnum] <- lapply(dat[isnum], function(x) ifelse(x > 1, ceiling(x), round(x, 0)))
dat
#   dwarf_shrub equisetum forb fungi graminoid lichen moss shrub_forb tall shrub tree
# 1       site1        34    0    22         1     36    4         60    0    35    0
# 2       site2        31    0    15         0     26    8         63    2    27    0
# 3       site3        50    0    10         0     24    4         65    7    19    0
# 4       site4        46    0     8         0     33    3         34    5    12    1
# 5       site5        28    0    11         0     40    6         30    0    38    0
# 6       site6        41    0    11         0      6    7          8    4     9    2

Data: I had to rename some of the columns since some of your column names are not as easy to read in as easily (spaces, hyphens).

dat <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
           dwarf_shrub equisetum  forb fungi graminoid lichen  moss shrub_forb tall shrub tree
site1            33.25         0 21.25   1.0     35.25   3.25 60.00       0.00      34.25 0.25
site2            30.25         0 15.00   0.0     25.75   7.50 62.25       1.50      26.75    0
site3            50.00         0 10.00   0.5     23.50   3.25 65.00       6.75      18.50    0
site4            46.00         0  7.75   0.0     32.75   2.25 33.75       4.50      11.25 0.75
site5            28.00         0 11.00   0.0     40.00   6.00 30.00       0.00      38.00    0
site6            40.25         0 10.50   0.0      5.75   6.25  7.25       3.25       8.75 1.25")

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