是否有替代的r基函数pivot_wider?

bq9c1y66  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(80)

我有以下类型的表:
| 经济特区|类|瓦尔|
| --|--|--|
| 1_1_1| 1 | 2 |
| 1_1_1| 5 | 2 |
| 1_1_2| 5 | 2 |
| 1_1_3| 1 | 1 |
| 1_1_3| 5 | 2 |
| 1_1_4| 1 | 1 |
| 1_1_5| 2 | 1 |
| 1_2_1| 1 | 2 |
| 1_2_1| 5 | 2 |
为了将列“Class”扩展到多个新列中,从“Val”列获取值,我使用了pivot_wider,一切都很顺利。我输入了以下代码:
第一个月
得到这样的结果:
| 经济特区| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| --|--|--|--|--|--|--|--|
| 1_1_1| 2 | 0 | 0 | 0 | 2 | 0 | 0 |
| 1_1_2| 0 | 0 | 0 | 0 | 2 | 0 | 0 |
| 1_1_3| 1 | 0 | 0 | 0 | 2 | 0 | 0 |
不幸的是,我必须使用一台外部计算机,那里只有基本的R软件包,并且请求额外软件包的时间不短。
我尝试使用这个解决方案:
newdata <- xtabs(dat$Val ~ dat$Sez + dat$Class)
但它给了我每行的频率分布:
| 经济特区|类|Freq|
| --|--|--|
| 1_1_1| 1 | 2 |
| 1_1_2| 1 | 0 |
| 1_1_3| 1 | 1 |
| 1_1_4| 1 | 1 |
| 1_1_5| 1 | 0 |
| 1_2_1| 1 | 1 |
我正在寻找一个解决方案,它使用R的基本函数,给我一个与使用pivot_wider得到的对象相等的对象。

noj0wjuj

noj0wjuj1#

我们将'Class'创建为factor并使用xtabs

df1$Class <- factor(df1$Class, levels = 1:7)

 xtabs(Val ~ SEZ + Class, df1)

字符串

  • 输出
Class
SEZ     1 2 3 4 5 6 7
  1_1_1 2 0 0 0 2 0 0
  1_1_2 0 0 0 0 2 0 0
  1_1_3 1 0 0 0 2 0 0
  1_1_4 1 0 0 0 0 0 0
  1_1_5 0 1 0 0 0 0 0
  1_2_1 2 0 0 0 2 0 0


如果我们需要data.frame输出,

out <- as.data.frame.matrix( xtabs(Val ~ SEZ + Class, df1))
out$SEZ <- row.names(out)
row.names(out) <- NULL

数据

df1 <- structure(list(SEZ = c("1_1_1", "1_1_1", "1_1_2", "1_1_3", "1_1_3", 
"1_1_4", "1_1_5", "1_2_1", "1_2_1"), Class = c(1L, 5L, 5L, 1L, 
5L, 1L, 2L, 1L, 5L), Val = c(2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L)), row.names = c(NA, -9L), class = "data.frame")

bgibtngc

bgibtngc2#

另一个使用reshape + merge的基本R选项

reshape(
  merge(df,
    expand.grid(
      SEZ = unique(df$SEZ),
      Class = 1:7
    ),
    all = TRUE
  ),
  direction = "wide",
  idvar = "SEZ",
  timevar = "Class"
)

字符串

SEZ Val.1 Val.2 Val.3 Val.4 Val.5 Val.6 Val.7
1  1_1_1     2    NA    NA    NA     2    NA    NA
8  1_1_2    NA    NA    NA    NA     2    NA    NA
15 1_1_3     1    NA    NA    NA     2    NA    NA
22 1_1_4     1    NA    NA    NA    NA    NA    NA
29 1_1_5    NA     1    NA    NA    NA    NA    NA
36 1_2_1     2    NA    NA    NA     2    NA    NA

57hvy0tb

57hvy0tb3#

当“Values”列包含整数时会发生什么?
我尝试将@akran解决方案(上面发布的)应用到这个表(碰巧在“Values”列中有字符串而不是整数)。

数据

"1_1_4", "1_1_5", "1_2_1", "1_2_1"), Class = c(1L, 5L, 5L, 1L, 
5L, 1L, 2L, 1L, 5L), Val = c(2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L)), row.names = c(NA, -9L), class = "data.frame")

字符串
运行此命令将导致错误:

xtabs(Val ~ SEZ + Class, df1)


x1c 0d1x的数据
我们需要将输出列“瓦尔”转换为数值,然后将其返回为文本。此脚本完成了这项工作(R版本4.3.1(2023-06-16)&djr 1.1.2):

# create list to translate
list_mapTclass <- seq(1:length(unique(df1$Val)))
print(length(list_mapTclass))
names(list_mapTclass) <-unique(df1$Val)

list_mapTclass[1:3]

df1$idxVal <- sapply(df1$Val, function(x) list_mapTclass[[x]] )

#essential! conver to factor
#df1$idxVal <- factor(df1$idxVal)

#df1

#xtabs(idxVal ~ SEZ + Class, df1)
wide_df_matrix <-xtabs(idxVal ~ SEZ + Class, df1)
 


#print(list_mapTclassInverted[1:3])
#--- add subid
wide_df0 <- as.data.frame.matrix( wide_df_matrix) %>% 
  setNames(paste0('col_', names(.))) #%>%

wide_df0
# reconvert values into the therapyclass content

# inverted list:

list_mapTclassInverted <- setNames(names(list_mapTclass), list_mapTclass)
# sometimes if you dont have 0s , you have to include it in the "list_mapTclassInverted" , as "Unknown"
#list_mapTclassInverted[['0']] <- 'Unknown'
print(list_mapTclassInverted)
wide_df0


# Next piece of code is to go through the full table and convert all the integers into their text
### VERY USEFUL! (Thanks to: @Martin Morgan - https://stackoverflow.com/questions/7547597/dictionary-style-replace-multiple-items )

wide_df00 <- wide_df0
wide_df00[] <- list_mapTclassInverted[unlist(wide_df00)]

wide_df00
## convert to a dataframe

wide_df0$SEZ <- row.names(wide_df0) 
row.names(wide_df0) <- NULL

# See the output
wide_df0


测试结果:

我将写另一篇文章(here)与此问题的情况下,这一个不完全匹配的问题.希望这有帮助!有时是耗时的事情在R基地,但正如问题中提到的,当你不得不使用没有更新的技术,你不是IT管理员,没有其他方法(特别是如果你必须在明天交付!).

相关问题