所以,我想在底部的另一列中合并一列。这就是我的数据的样子:
V1 V2 V3 V4
-75.6364 -33.3363 -68.6320 -35.7657
-62.4546 -42.6754 -60.0532 -41.7773
-53.1363 -48.3385 -56.0956 -47.4524
-51.9633 -37.6143 -60.3062 -54.7817
我想要这个,合并V1-V3和V2-V4:
V1 V2
-75.6364 -33.3363
-62.4546 -42.6754
-53.1363 -48.3385
-51.9633 -37.6143
-68.6320 -35.7657
-60.0532 -41.7773
-56.0956 -47.4524
我一直在寻找这个页面,但我发现了cbind
和rbind
个选项
但不要提供我想要的东西。
答案 0 :(得分:3)
data.table的melt
函数特别好用,因为它可以将参数分组到并行列中。它将插入一个variable
标识符列,但您可以将其分组。
library(data.table)
melt(setDT(df), # set df to a data.table
measure.vars = list(c(1,3), c(2,4)), # set column groupings
value.name = 'V')[ # set output name scheme
, -1, with = F] # subset out variable column
## V1 V2
## 1: -75.6364 -33.3363
## 2: -62.4546 -42.6754
## 3: -53.1363 -48.3385
## 4: -51.9633 -37.6143
## 5: -68.6320 -35.7657
## 6: -60.0532 -41.7773
## 7: -56.0956 -47.4524
## 8: -60.3062 -54.7817
在基数R中,stack
函数熔化为长,但不是并行列。但是,您可以将列名称转换为要用于unstack
的索引,或者仅使用rep(rep(1:2, each = 4), 2)
等手动制作合适的矢量。
df2 <- stack(df)
# change ind (variable/key/index) column to number, take modulo 2 to separate even/odd,
# and invert 0/1 add 1 to make better column names
df2$ind <- 1 + !as.numeric(df2$ind) %% 2
unstack(df2)
## X1 X2
## 1 -75.6364 -33.3363
## 2 -62.4546 -42.6754
## 3 -53.1363 -48.3385
## 4 -51.9633 -37.6143
## 5 -68.6320 -35.7657
## 6 -60.0532 -41.7773
## 7 -56.0956 -47.4524
## 8 -60.3062 -54.7817
答案 1 :(得分:3)
其他答案很有帮助,但每个答案都必须在每对列中进行字面编码。这是一个通用函数,下面有十列更严格的例子:
public async Task<User> GetAsyncADUser(PrincipalContextParameter param)
{
try
{
if (UseLDAPForIdentityServer3)
{
using (var pc = new PrincipalContext(ContextType.Domain, param.ADDomain, param.ADServerContainer, param.ADServerUser, param.ADServerUserPwd))
{
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(pc, param.UserNameToValidate);
if (userPrincipal != null)
{
bool isvalid = pc.ValidateCredentials(userPrincipal.DistinguishedName, param.UserPasswordToValidate, ContextOptions.SimpleBind);
if (isvalid)
{
User user = new User { ad_guid = userPrincipal.Guid.ToString(), Username = param.UserNameToValidate, Password = param.UserPasswordToValidate };
return user;
}
}
}
}
}
catch (Exception ex)
{
throw;
}
return null;
}
修改强>
使用Ananda的技术in this question,我们可以创建一个单行:
dfSeq <- function(df) Map(':', 1:(ncol(df)-1), 2:ncol(df))[c(TRUE,FALSE)]
stackDF <- function(df1) do.call('rbind', Map(function(x,y)
setNames(x[y], names(df1)[1:2]), list(df1), dfSeq(df1)))
mydf <- as.data.frame(replicate(10, rnorm(10)))
dim(mydf)
[1] 10 10
stackDF(mydf)
# V1 V2
# 1 1.031131668 -1.00884258
# 2 1.803293498 2.15713217
# 3 -1.295998573 0.20951434
# 4 -1.314269143 1.56329500
# 5 -1.138388270 -0.87029891
# 6 -1.312514370 0.31815244
# 7 1.436570621 -0.96508931
答案 2 :(得分:2)
看起来你想要堆叠每对列。在下面的代码中,我们只是将第3列和第4列的名称更改为第1列和第2列,以便rbind
可以根据需要使用:
new.df = rbind(df[,1:2], setNames(df[,3:4], names(df[,1:2])))
new.df
V1 V2 1 -75.6364 -33.3363 2 -62.4546 -42.6754 3 -53.1363 -48.3385 4 -51.9633 -37.6143 5 -68.6320 -35.7657 6 -60.0532 -41.7773 7 -56.0956 -47.4524 8 -60.3062 -54.7817