简体   繁体   English

如何将 ifelse 函数应用于数据框中的所有单元格?

[英]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.我正在尝试将 ifelse 语句应用于数据框中的所有单元格。 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.我试图将数字四舍五入到最接近的整数,以便在值大于 1 时使用 round() 函数,在值小于 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()).我没有成功应用语句的后半部分 (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 .您提到了ifelse ,我认为使用lapply将其应用于每一列是很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检查,如果您的数据始终为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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能将函数应用得狭窄 - How can i apply a function with an ifelse stricture 如何将与data.frame单元格一起使用的函数应用于data.frame列 - How to apply a function that works with data.frame cells to data.frame columns 我可以使用 ifelse 函数对数据框进行切片吗? - Can I slice a data frame using an ifelse function? 如何使用dplyr的filter()和R-base的ifelse()过滤数据框? - How do I filter a data frame with dplyr's filter() and R-base's ifelse()? 如何在保留整个数据帧的同时将函数应用于数据的子集? - How do I apply a function to a subset of my data while retaining the entire data frame? 如何在数据帧上使用ifelse - how to use ifelse on a data frame 如何将 function 应用于数据框中的所有行组合? - How to apply a function to all combinations of rows in a data frame? 如何将函数映射到列表中每个数据框的每一行? - How do I have mapply apply the function to each row of each data frame in a list? 如何将R中的函数应用于由另一列分组的数据帧的某些列? - How do I apply a function in R to certain columns of a data frame grouped by another column? 如何使用ddply将函数应用于按数据帧的另一列中的每个因子划分的一列? - How do I apply a function to one column split by each factor in another column of a data frame using ddply?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM