gpt4 book ai didi

r - 如何用spss文件中的data.frames中的标签替换值?

转载 作者:行者123 更新时间:2023-12-01 11:11:43 26 4
gpt4 key购买 nike

我必须阅读一个 sav 文件
我使用包 haven

library(haven)
dataset<- read_sav("datafile.sav")
在控制台中,我可以看到标签:
enter image description here
        dput(head(voyages$portdep))
structure(c(50422, 50299, 50299, 50299, NA, NA), label = "Port of departure", labels = c(Alicante = 10101,
Barcelona = 10102, Bilbao = 10103, Cadiz = 10104, Figuera = 10105,
Gibraltar = 10106, `La Coruña` = 10107, Santander = 10110, Seville = 10111,
`San Lucar` = 10112, Vigo = 10113, `Spain, port unspecified` = 10199,
Lagos = 10202, Lisbon = 10203, Oporto = 10204, `Ilho do Fayal` = 10205,
Setubal = 10206, `Portugal, port unspecified` = 10299, `Great Britain, port unspecified` = 10399,
Barmouth = 10401, Bideford = 10402, Birkenhead = 10403, Bristol = 10404,
Brixham = 10405, Broadstairs = 10406, Cawsand = 10407, Chepstow = 10408,
Chester = 10409, Colchester = 10410, Cowes = 10411, Dartmouth = 10412,
Deptford = 10413, Dover = 10414, Exeter = 10415, Folkstone = 10416,
Frodsham = 10417, Gainsborough = 10418, Greenwich = 10419, Guernsey = 10420,
Harwich = 10421, Hull = 10422, Ilfracombe = 10423, Ipswich = 10424,
`Isle of Man` = 10425, `Isle of Wight` = 10426, Jersey = 10427,
Kendal = 10428, `King's Lynn` = 10429, Lancaster = 10430, Lindale = 10431,
Liverpool = 10432, London = 10433, Lyme = 10434, Maryport = 10436,
`Milford Haven` = 10437, `New Shoreham` = 10438, `Newcastle upon Tyne` = 10439,
Newnham = 10440, `North Shields` = 10441, Norwich = 10443, Padstowe = 10444,
Parkgate = 10445, `Piel of Foulney` = 10446, Plymouth = 10447,
Poole = 10448, Portsery = 10449, Portsmouth = 10450, Poulton = 10451,
Preston = 10452, Ramsgate = 10453, Ravenglass = 10454, `River Thames` = 10455,
Rochester = 10456, Rotherhithe = 10457, Rye = 10458, Scarborough = 10459,
Sheerness = 10460, Shields = 10461, Shoreham = 10462, Sidmouth = 10463,
Southampton = 10464, Stockton = 10466, Stockwithe = 10467, Sunderland = 10468,
Teignmouth = 10469, Topsham = 10470, Torbay = 10471, Wales = 10472,
在 html 表中,我只有值:
enter image description here
如何通过来自spss文件的data.frames中的标签替换值?用于在html表中显示?
使用 sjlabelled 包,我可以获得任何列的标签:
library(sjlabelled)
get_labels(voyages$portdep)
1]“阿利坎特”“巴塞罗那”“毕尔巴鄂”“加的斯”
[5] 《菲格拉》《直布罗陀》《拉科鲁尼亚》《桑坦德》
[9] "Seville""San Lucar""Vigo""Spain, port unspecified"
[13]“拉各斯”“里斯本”“波尔图”“Ilho do Fayal”
[17] “塞图巴尔” “葡萄牙,未指定港口” “英国,未指定港口” “巴茅斯”
[21]“比德福德”“伯肯黑德”“布里斯托尔”“布里克瑟姆”
[25] 《Broadstairs》《Cawsand》《Chepstow》《Chester》
[29] 《科尔切斯特》《考斯》《达特茅斯》《德特福德》
[33] 《多佛》《埃克塞特》《福克斯通》《弗罗德沙姆》
[37] 《盖恩斯伯勒》《格林威治》《根西岛》《哈里奇》
[41] 《赫尔》《伊尔弗勒科姆》《伊普斯维奇》《马恩岛》
[45]《怀特岛》《泽西》《肯德尔》《国王的林恩》
我试过 :
在单列上:
  dataset2 <- dataset %>% mutate(portdep = get_labels(portdep))

Erreur : Column portdep must be length 36002 (the number of rows) orone, not 847


在所有数据帧上:
  dataset2 <-  dataset %>% mutate_all(funs(get_labels(.)))
在第一列上出现相同的错误:

Column xxx must be length 36002 (the number of rows) or one, not 2

最佳答案

我认为您可以通过使用 haven::as_factor 获得您想要的东西。 .

这行得通吗?

library(haven)
library(dplyr)

dataset %>%
mutate_all(as_factor) %>%
head() %>%
View()

关于r - 如何用spss文件中的data.frames中的标签替换值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com