简体   繁体   English

在数据表 R 中,如何创建一个新变量,该变量为特定观察值取特定值?

[英]In data table, R, how do I make a new variable that takes a certain value for specific observations?

In data table, i am trying to make a variable that takes a value when a certain observation has been met.在数据表中,我试图创建一个在满足某个观察值时取值的变量。

smoked <- matrix(c("A","A","A","B","B","B","A","B","A"),ncol=3,byrow=TRUE)
colnames(smoked) <- c("Type","Name","cusip")
rownames(smoked) <- c("A","B","C")
smoked <- as.table(smoked)
smoked

How would i create a another column that that responds "B" every occasion the condition is met within the "name" columnn.. and then "not B" for every occasion it does not.我将如何创建另一个列,该列在每次在“名称”列中满足条件时都响应“B”。然后在每次不满足的情况下都响应“非 B”。

Try using cbind() with the ifelse() function.尝试将cbind()ifelse() function 一起使用。

smoked <- matrix(c("A","A","A","B","B","B","A","B","A"),ncol=3,byrow=TRUE)
colnames(smoked) <- c("Type","Name","cusip")
rownames(smoked) <- c("A","B","C")
smoked <- as.table(smoked)

# use cbind
smokedNew <- cbind(smoked, newCol = ifelse(test = smoked[,"Name"] == "B", yes = "B", no = "not B"))

smokedNew

#   Type Name cusip newCol 
# A "A"  "A"  "A"   "not B"
# B "B"  "B"  "B"   "B"    
# C "A"  "B"  "A"   "B"  

If the data comes as data frame, it is straight forward:如果数据以数据框的形式出现,那就直截了当:

#create data frame
smoked <- data.frame( Type=c("A","B","A"), Name=c("A","B","B"), cusip=c("A","B","A"))

# test function
my.test <- function(test.value, test.on) { paste0(ifelse(test.value==test.on,'','not '), test.on) }

# now testing on content of Name colum
smoked$res1 <- sapply( smoked$Name, FUN=my.test, test.on='B' )
smoked$res2 <- sapply( smoked$Name, FUN=my.test, test.on='C' )

which yields:产生:

> print(smoked)
  Type Name cusip  res1  res2
1    A    A     A not B not C
2    B    B     B     B not C
3    A    B     A     B not C

暂无
暂无

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

相关问题 如何使用现有的虚拟变量创建一个新变量,该变量对组内的某些先导观察值取值 1 - How to use an existing dummy variable to create a new one that takes the value 1 for certain lead observations within a group 如何基于观察组的另一个变量为观察组创建一个新变量 - How do I create a new variable for a group of observations based on another variable specific to that group R:如何在 data.table 中标记特定时间范围内的观察? - R: How to flag observations within a certain timeframe in data.table? R:创建等于另一个变量(特定年份)的特定观察值的新变量 - R: Create new variable equal to specific observations of another variable (certain years) 根据 R 中变量的特定值删除观察值 - Delete observations base on specific value of variable in R 如何从 R 中的列进行新观察? - How can I make new observations from columns in R? 如何过滤 R 中 data.frame 中低于某个特定观察值的观察值? - How do I filter observations that come below one specific observation in a data.frame in R? 在一个变量中对多个观察值进行分类,以便我可以将它们分类到新列中。 我怎样才能使代码更短? 在 R - classifying multiple observations within one variable so then i can categorise them in new column . how can i make the code shorter? in R R:如何按组计算变量对特定值的百分比? - R: how can I calculate the percentages a variable takes on a certain value by group? 如何将表格变成 R 中具有各种属性的观察列表? - How do I turn a table into a list of observations with various attributes in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM