通过将向量与数据帧匹配来创建向量[R]

时间:2022-06-14 16:22:13

I have the following dataframe:

我有以下数据帧:

> zCode <- sample(50:150, size = 10, replace = TRUE)
> x <- sample(50:150, size = 10, replace = TRUE)
> test <- data.frame(x,zCode )
> test
  zCode  x
1  110 114
2  108 150
3   57 100
4   53  98
5  114  67
6  143 126
7  110  95
8  106 101
9  103  70
10 149  73

I also have this vector:

我也有这个载体:

> z <- c(53, 57, 110)
> z
[1]  53  57 110

I want to create a new dataframe based on vector Z, that pulls the maximum x value associated with that z-code, like so:

我想基于向量Z创建一个新的数据帧,它会拉出与该z代码相关的最大x值,如下所示:

 Z   x
53   98
57   100
110  114

2 个解决方案

#1


2  

Here are some possibilities. They do not use any packages.

这是一些可能性。他们不使用任何包裹。

1) For each element of z compute the subset of rows in test with that zCode and then take the maximum of each x:

1)对于z的每个元素,使用该zCode计算测试中的行的子集,然后取每个x的最大值:

data.frame(z, x = sapply(z, function(z) max(subset(test, z == zCode)$x)))

giving:

赠送:

    z   x
1  53  98
2  57 100
3 110 114

2) Another approach is to use aggregate to find all the maxima and the merge with z to get just those:

2)另一种方法是使用聚合来查找所有最大值并与z合并以获得这些:

merge(data.frame(z), aggregate(x ~ zCode, test, max), by = 1, all.x = TRUE)

giving:

赠送:

    z   x
1  53  98
2  57 100
3 110 114

Hote: The input used, in reproducible form, is:

Hote:使用的可重复形式的输入是:

Lines <- "
  zCode  x
1  110 114
2  108 150
3   57 100
4   53  98
5  114  67
6  143 126
7  110  95
8  106 101
9  103  70
10 149  73"
test <- read.table(text = Lines)

z <- c(53, 57, 110)

#2


0  

Here is a data.table solution:

这是一个data.table解决方案:

# Original data
dt <- data.table(zCode = c(110, 108, 57, 53, 114, 143, 110, 106, 103, 149), 
                 x     = c(114, 150, 100, 98, 67, 126, 95, 101, 70, 73))
z <- c(53, 57, 110)

# a new dataframe based on vector z
dt[zCode %in% z, max(x), by = zCode]
   zCode  V1
1:   110 114
2:    57 100
3:    53  98

EDIT:

编辑:

# Keeps the columns names unchanged
dt[zCode %in% z, .(x = max(x)), by = zCode]
   zCode   x
1:   110 114
2:    57 100
3:    53  98

#1


2  

Here are some possibilities. They do not use any packages.

这是一些可能性。他们不使用任何包裹。

1) For each element of z compute the subset of rows in test with that zCode and then take the maximum of each x:

1)对于z的每个元素,使用该zCode计算测试中的行的子集,然后取每个x的最大值:

data.frame(z, x = sapply(z, function(z) max(subset(test, z == zCode)$x)))

giving:

赠送:

    z   x
1  53  98
2  57 100
3 110 114

2) Another approach is to use aggregate to find all the maxima and the merge with z to get just those:

2)另一种方法是使用聚合来查找所有最大值并与z合并以获得这些:

merge(data.frame(z), aggregate(x ~ zCode, test, max), by = 1, all.x = TRUE)

giving:

赠送:

    z   x
1  53  98
2  57 100
3 110 114

Hote: The input used, in reproducible form, is:

Hote:使用的可重复形式的输入是:

Lines <- "
  zCode  x
1  110 114
2  108 150
3   57 100
4   53  98
5  114  67
6  143 126
7  110  95
8  106 101
9  103  70
10 149  73"
test <- read.table(text = Lines)

z <- c(53, 57, 110)

#2


0  

Here is a data.table solution:

这是一个data.table解决方案:

# Original data
dt <- data.table(zCode = c(110, 108, 57, 53, 114, 143, 110, 106, 103, 149), 
                 x     = c(114, 150, 100, 98, 67, 126, 95, 101, 70, 73))
z <- c(53, 57, 110)

# a new dataframe based on vector z
dt[zCode %in% z, max(x), by = zCode]
   zCode  V1
1:   110 114
2:    57 100
3:    53  98

EDIT:

编辑:

# Keeps the columns names unchanged
dt[zCode %in% z, .(x = max(x)), by = zCode]
   zCode   x
1:   110 114
2:    57 100
3:    53  98