[英]How to create a new column with multiple values based on another column in R
I have a data frame in R called A.Data. 我在R中有一个数据帧,称为A.Data。
It has 8 different columns: plate
, row,
col
, TOF
, EXT
, green
, red
, and yellow
. 它具有8种不同的列:
plate
, row,
col
, TOF
, EXT
, green
, red
和yellow
。
Below is an example of the data. 以下是数据示例。
> head(A.Data)
plate row col TOF EXT green red yellow
1 1 A 12 20 21 2 0 0
2 1 C 12 20 17 0 1 0
3 1 C 11 20 17 0 0 1
4 1 A 10 20 16 1 1 3
5 1 A 10 20 16 0 0 0
6 1 A 10 20 15 0 0 0
I'm trying to add a new column to A.Data
called conc
(short for concentration). 我试图将新列添加到
A.Data
称为conc
(浓度的缩写)。 The new column called conc
depends on the value in the col column. 名为
conc
的新列取决于col列中的值。
-If col is 1 or 7, conc should equal to 0
-If col is 2 or 8, conc should equal to 0.5
-If col is 3 or 9, conc should equal to 1
-If col is 4 or 10, conc should equal to 2
-If col is 5 or 11, conc should equal to 4
-If col is 6 or 12, conc should say NA
So for the first 6 rows of data, the conc
column should say NA, NA, 4, 2, 2, 2
because the col
column values for the first 6 rows are 12, 12, 11, 10, 10, 10
. 因此,对于前6行数据,所述
conc
柱应该说NA, NA, 4, 2, 2, 2
,因为col
用于第一行6列的值是12, 12, 11, 10, 10, 10
。
I asked my professor for help and he gave me this hint: 我向我的教授寻求帮助,他给了我这个提示:
df$newcol <- rep(1, 1000)
will add a new column to the df
data frame called newcol
and will have 1 replicated 1000 times df$newcol <- rep(1, 1000)
将向df
数据帧添加一个称为newcol
的新列,并将复制1次1000次
Try to add a concentration column called conc
with 0, 0.5, 1, 2, 4, NA
replicated as many times as you need for the entire column. 尝试添加一个浓度为
conc
的浓度列,该浓度列根据整个列的需要重复复制0, 0.5, 1, 2, 4, NA
。
Here is the summary of A.Data$col
, in case you might find it useful... 这是
A.Data$col
的摘要,以防您发现它有用...
> summary (A.Data$col)
1 2 3 4 5 6 7 8 9 10 11 12 NA's
1128 703 538 256 156 30 2101 1039 741 294 73 60 11
Thank you! 谢谢!
Not tested, but this may work 未经测试,但这可能有效
map_column <- rep(c(0, 0.5, 1, 2, 4, NA),2)
df$newcol <- map_column[df$col]
EDIT: The idea behind this code is: map_column
, which is a vector of length 12, serves here as a map (in the mathematical sense) between the numbers 1 to 12 and the values in the vector. 编辑:此代码背后的想法是:
map_column
,它是长度为12的向量,在这里用作数字1到12与向量中的值之间的映射(在数学意义上)。 For instance, 例如,
map_column[[1]]
returns the first element of the vector (0), and 返回向量(0)的第一个元素,并且
map_column[[9]]
returns the 9th element of the vector (1), and so on. 返回向量(1)的第9个元素,依此类推。 Now R vectors have the capability to process several inputs at once, so that
现在,R向量具有一次处理多个输入的能力,因此
map_column[c(1,9)]
returns the corresponding elements ( c(0,1)
) at these positions in one go. 一次性返回这些位置上的对应元素(
c(0,1)
)。 Note that it is important to use a single square bracket [
instead of [[
here. 请注意,使用单个方括号
[
而不是[[
这里。
This works. 这可行。
convert <- function(number){
if(number == 1 | number == 7){return(0)}
if(number == 2 | number == 8){return(.5)}
if(number == 3 | number == 9){return(1)}
if(number == 4 | number == 10){return(2)}
if(number == 5 | number == 11){return(4)}
if(number == 6 | number == 12){return(NA)}
}
A.Data$newcol <- do.call(rbind, lapply(A.Data$col, convert))
Use merge. 使用合并。
augment <- data.frame(col=1:12,conc=rep(c(0, 0.5, 1, 2, 4, NA),2))
A.Data <-merge(A.Data,augment,by="col",sort=F)
A.Data
# col plate row TOF EXT green red yellow conc
# 1 12 1 A 20 21 2 0 0 NA
# 2 12 1 C 20 17 0 1 0 NA
# 3 11 1 C 20 17 0 0 1 4
# 4 10 1 A 20 16 1 1 3 2
# 5 10 1 A 20 16 0 0 0 2
# 6 10 1 A 20 15 0 0 0 2
This creates an an augment dataframe with 2 columns, col
corresponding to col
in A.Data
, and conc
with the augment. 这产生与2列中,一个数据帧扩充
col
对应于col
在A.Data
,和conc
与扩充。 Then merge that with A.Data based on col. 然后将其与基于col的A.Data合并。
Here's a very different approach based on mathematical and logical operations: 这是一种基于数学和逻辑运算的非常不同的方法:
x <- c(1:12, NA) # an example vector including all possible values
floor(2 ^ (z <- x %% 6 - 2)) / 2 * (z + 2 | NA)
The result: 结果:
[1] 0.0 0.5 1.0 2.0 4.0 NA 0.0 0.5 1.0 2.0 4.0 NA NA
(I fear that this solution may appear like obfuscation.) (我担心这种解决方案可能看起来像是迷惑。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.