R 操作数据框中的一些数值列,这些列也包含字符

R 操作数据框中的一些数值列,这些列也包含字符,r,data-manipulation,R,Data Manipulation,我有一个包含字符和数字列的数据框。在一些数字列中,我想测试该值是否大于1,如果大于1,我想将其更改为1 我已经设法将所有不同的值从0变为1,但其中包括字符和一列,我想保持不变 示例数据帧: > species<- c("Pinus halepensis", "Majorana syriaca", "Iris > palaestina","Velezia fasciculata") > rar

我有一个包含字符和数字列的数据框。在一些数字列中,我想测试该值是否大于1,如果大于1,我想将其更改为1

我已经设法将所有不同的值从0变为1,但其中包括字符和一列,我想保持不变

示例数据帧:

> species<- c("Pinus halepensis", "Majorana syriaca", "Iris
> palaestina","Velezia fasciculata") 
> rarness<- c("F", "CC", "F", "O")
> endangered<-c(0,0,0,6.8) plot1<- c(1,2,1,1) plot2<- c(0,1,0,0)
> df<-as.data.frame(cbind(species, rarness, endangered, plot1, plot2))

>物种稀有性主要是濒危物种,这是因为您的数据框列中有一些因素。在将列转换为1/0之前,需要先将列更改为数字

library(dplyr)

df %>%
 mutate_at(vars(plot1, plot2), ~as.integer(as.numeric(as.character(.)) > 1))

#              species rarness endangered plot1 plot2
#1    Pinus halepensis       F        0.0     0     0
#2    Majorana syriaca      CC        0.0     1     0
#3     Iris palaestina       F        0.0     0     0
#4 Velezia fasciculata       O        6.8     0     0
或类似的使用基础R

df[4:5] <- lapply(df[4:5], function(x) as.integer(as.numeric(as.character(x)) > 1))
df[4:5]1)

大多数情况下,这是因为您的数据帧列中有因子。在将列转换为1/0之前,需要先将列更改为数字

library(dplyr)

df %>%
 mutate_at(vars(plot1, plot2), ~as.integer(as.numeric(as.character(.)) > 1))

#              species rarness endangered plot1 plot2
#1    Pinus halepensis       F        0.0     0     0
#2    Majorana syriaca      CC        0.0     1     0
#3     Iris palaestina       F        0.0     0     0
#4 Velezia fasciculata       O        6.8     0     0
或类似的使用基础R

df[4:5] <- lapply(df[4:5], function(x) as.integer(as.numeric(as.character(x)) > 1))
df[4:5]1)

您也可以在应用条件之前进行复制。您需要指定只处理这些列的列。如果只有两列,则可以这样手动操作:

# Create copy
test <- df

# Update specific column
test$plot1[(as.numeric(test$plot1)) > 1]  <- 1
test$plot2[(as.numeric(test$plot2)) > 1]  <- 1
test
#               species rarness endangered plot1 plot2
# 1    Pinus halepensis       F          0     1     0
# 2    Majorana syriaca      CC          0     1     1
# 3     Iris palaestina       F          0     1     0
# 4 Velezia fasciculata       O        6.8     1     0

我使用
tail
选择索引4之后的所有列名称(例如,删除索引3之前的所有元素)。关于如何子集列表

您也可以在应用条件之前进行复制。您需要指定只处理这些列的列。如果只有两列,则可以这样手动操作:

# Create copy
test <- df

# Update specific column
test$plot1[(as.numeric(test$plot1)) > 1]  <- 1
test$plot2[(as.numeric(test$plot2)) > 1]  <- 1
test
#               species rarness endangered plot1 plot2
# 1    Pinus halepensis       F          0     1     0
# 2    Majorana syriaca      CC          0     1     1
# 3     Iris palaestina       F          0     1     0
# 4 Velezia fasciculata       O        6.8     1     0

我使用
tail
选择索引4之后的所有列名称(例如,删除索引3之前的所有元素)。关于如何子集列表

欢迎来到SO!你为什么说变异选项不起作用?您是否收到错误消息,或者结果不是您预期的结果?请记住,您首先需要
library(dplyr)
。您好,我使用的library(tidyverse)总线确实收到一条警告消息:“在Ops.factor(plot1,1):”>“对于factors没有意义”。此外,如果这确实有效,if将仅适用于plot1,而我需要将其应用于真实数据集中的许多列。。。谢谢,伊丹。欢迎来到苏!你为什么说变异选项不起作用?您是否收到错误消息,或者结果不是您预期的结果?请记住,您首先需要
library(dplyr)
。您好,我使用的library(tidyverse)总线确实收到一条警告消息:“在Ops.factor(plot1,1):”>“对于factors没有意义”。此外,如果这确实有效,if将仅适用于plot1,而我需要将其应用于真实数据集中的许多列。。。谢谢你,伊丹。谢谢你的回答!但事实上,我的原始数据集中有许多列(~35)。是否有一种方法可以使用范围或其编号来处理所有相关列?您希望如何选择要处理的列?
plot1 plot2
之后的所有列?濒危之后的所有列(因此也包括plot1和plot2)都会查看更新。在这里,我使用
tail
函数选择列索引之后的所有列。您可以自由设计自己的函数来选择要处理的适当列。除了在使用“我的数据”时将新值重新分配回df之外,所有这些都起到了作用。我得到了这个错误警告:“
[谢谢你的回答!但事实上,我的原始数据集中有许多列(~35)。有没有办法使用范围或它们的编号来处理所有相关列?你想如何选择要处理的列?
plot1 plot2
之后的所有列?plot2之后的所有列(因此也包括plot1和plot2)查看更新。在这里,我使用
tail
函数选择列索引后的所有列。请随意设计您自己的函数,以选择要处理的适当列。除了使用我的数据时将新值重新分配回df之外,所有这些都起到了作用。我收到了以下错误警告:
[