将一列数组转换为 R 中的单个值列 [英] Transforming a column of arrays to column of single values in R

查看:34
本文介绍了将一列数组转换为 R 中的单个值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列数组,我想将它们分配给各个单元格.例如,我的列目前看起来像这样:

I have two columns of arrays that I would like to give individuals cells to. For example, my columns currently look like this:

<头>
NDVI日期
[0.1,0.5,0.7][1.54E12, 1.54E12, 1.54E12]

我如何才能将其重新格式化为:

How can I take this and reformat it to:

<头>
NDVI日期
0.11.54E12
0.51.54E12
0.71.54E12

以下是一些示例数据:

<代码> DF< - 结构(列表(NDVIs = C(" [0.0048532285033119] QUOT;," [0.031866426092985685,0.0346768172783618,0.05690584716758292,0.05963840093735323,0.049984507125651834,0.05225985971236913,0.05248901349444936,0.05518163825042716,-0.010183523082805207,-0.001665440442765531,0.03987908018516375,0.04187215406831169,0.08414429526637883,0.08715243094329189,-0.005957091678983289,-0.009527640910524507,0.010492166097587687,0.026020594673257052,0.002104522074938855,0.003507257657688491,0.06828452153139898,0.07606416388481754,0.06883156241096627,0.07718980197474816,0.06544808609317253,0.07474777221051583,0.10378917503028039,0.11123383912244203,0.05613527636023197,0.06331642130827059,0.06924611219668912,0.07666941206343858,0.06120038033501253,0.07040186289113536,0.0641993427579592,0.07613137403151546,0.02045492869437104,0.0331589149611709,0.07758880477807421,0.08753787069131283,0.07957631716729259,0.08952965234407557,0.0885082162194797,00.09848859431952328,-0.0020876210575472842,0.00246885618171861,0.10852078141016781,0.11765974025773,-0.026210051068679536,-0.017421882870782675,0.10807407562845053,0.11710267730586506,0.11281567251547685,0.12184561048692327,0.07186643839535878,0.07790008153091242,0.022632037271678205,0.0365365472967711] QUOT ;," [0.03529428376870003,0.009603157045076827,0.010105041611001576,0.022109721139215967,0.04320847194846547,0.06382705400539401,0.01733482969659961] QUOT ;," [0.05067307147600608,0.04650335539685854,0.04230580325409252,0.019941600277439516,0.03878433876933318,0.027594882066713636,0.032576179777168805,0.006187539680589851,0.0018615082099339225,0.042639901745788876,0.04555515255591705,0.05557934077121789,0.03141074118001885,-0.024594166196115066,0.06226281017212869,0.02775252318220509] QUOT ;," [0.22694578827413947,0.30629362044983743,0.26378399913683515,0.2582198975762179,0.25641849395215666,0.23752142045041516,0.23781654568851535,0.14139758147170572,0.2541489741719387,0.2419639056967162,0.2275033844898179,0.24470134691705184,0.22140420350534376,0.22465304694290103,0.21744809821978436,0.20724760985497206,0.19287780410696884] QUOT ;," [0.02437980239793386,0.046501630291612854,0.03515690678492754,0.03996495641681273,0.02565338402629212,0.02522057544087444,0.016355304453160305,0.010076012002045382,0.024906389352811975,0.040025952670788865,0.05043572220046672,0.0676703068532929,0.08040324690810197,0.011653761506917892,0.07901386245569544,0.14084622762340893,0.13315404483239526,0.14297013211925677] QUOT ;," [0.0696531704758789,0.06968717739514622,0.060495302532228894,0.060465281863641605,0.050631820224526766,0.05069794600355197,0.04996845148127263,0.050053255940312645,0.052401506092804355,0.0524735297646711,0.040763890820717345,0.04078582273414091,0.04106548840491197,0.04103821234740117,0.003195298620254946,-0.005673033858736476,-0.0018119432210428274,0.0015232350000433797,0.04386980266596469,0.044086120151773434,0.046401011127926925,0.04647825535002269,0.03833234785505313,0.03824161507110303,0.06037120877605433,0.06055975876554486,0.0592133770889435,0.05950182701095346,0.05530416996051339,0.05463335706292855,0.033097906541426636,0.02467463272473935,0.0580151510674638,0.05815851281926995,0.05560044632496825,0.05621848408403043] QUOT ;," [0.40218207770330455,0.3833707257903159,0.3498789365559936,0.3415851169240495,0.11857246658703605,0.09110434200774196,0.02929675584172094,0.13218157545304937,0.00568591277213531,0.2312335955409189,0.06118283356210741,0.23127376963601848,0.07698393284205111,0.05667080989824748,0.11169816100135183,0.018740255299188605,0.07543980191846927] QUOT ;," [0.04179551280868712,0.0383419807710962,0.0504998226331591,0.0011002529304645489,0.031002814569137,0.022506919207544558,0.03933692454785477,0.029375815795034198,0.04674437953418841,0.04864569790993668,0.036774224525189025,0.002999267453586999,0.03965538865956306,0.04553202255868567,0.012738284285019974,0.05355605970787331,0.03678091244450905] QUOT ;," [0.023817638054132135,0.02855430777145022,0.08243891605296151,0.014681590484211679,0.14321736621086276,0.07733703130141825,0.015297125457766068,0.10985901950753231,0.12587086535155378,0.13064566485984275,0.1297667968282656] QUOT;),日期= C(" [1.527502095E12] QUOT;," [1.544955875E12,1.544955875E12,1.545126378E12,1.545126378E12,1.545394367E12,1.545394367E12,1.545561339E12,1.545561339E12,1.545820928E12,1.545820928E12,1.545993046E12,1.545993046E12,1.546250253E12,1.546250253E12,1.546425701E12,1.546425701E12,1.546684843E12,1.546684843E12,1.546854495E12,1.546854495E12,1.547112218E12,1.547112218E12,1.547288454E12,1.547288454E12,1.547546364E12,1.547546364E12,1.547718166E12,1.547718166E12,1.547980939E12,1.547980939E12,1.548153141E12,1.548153141E12,1.548413046E12,1.548413046E12,1.548585114E12,1.548585114E12,1.548842378E12,1.548842378E12,1.549019277E12,1.549019277E12,1.549276707E12,1.549276707E12,1.549447219E12,1.549447219E12,1.549708493E12,1.549708493E12,1.549880513E12,1.549880513E12,1.550138768E12,1.550138768E12,1.550312936E12,1.550312936E12,1.55057479E12,1.55057479E12,1.550745218E12,1.550745218E12,1.551007067E12,1.551007067E12] QUOT ;,"[1.544868896E12, 1.545732881E12, 1.546164507E12, 1.546599228E12, 1.547030531E12, 1.547460976E12],8152E12" [1.562323826E12,1.562755476E12,1.563185103E12,1.563624031E12,1.56405523E12,1.564483439E12,1.564915837E12,1.565347309E12,1.565777661E12,1.566215882E12,1.566643642E12,1.567097272E12,1.567507638E12,1.568369457E12,1.56880789E12,1.569668195E12]"," [1.552392943E12,1.552819566E12,1.553251718E12,1.553682704E12,1.554112987E12,1.55454323E12,1.554975918E12,1.555408185E12,1.555841425E12,1.556271571E12,1.55670391E12,1.557135402E12,1.557568058E12,1.557998903E12,1.558863212E12,1.55929586E12, 1.559728791E12]"," [1.561454836E12,1.561888229E12,1.562320207E12,1.562751856E12,1.563181463E12,1.563618614E12,1.564046906E12,1.564480274E12,1.564912267E12,1.565343999E12,1.565773615E12,1.566210466E12,1.566639283E12,1.567072243E12,1.567504618E12,1.567936832E12, 1.568366042E12, 1.568802771E12]"," [1.561028124E12,1.561028124E12,1.561460566E12,1.561460566E12,1.561887116E12,1.561887116E12,1.562321887E12,1.562321887E12,1.562750997E12,1.562750997E12,1.563188512E12,1.563188512E12,1.563622208E12,1.563622208E12,1.564052361E12,1.564052361E12,1.564478817E12,1.564478817E12,1.564913968E12,1.564913968E12,1.565342227E12,1.565342227E12,1.565780829E12,1.565780829E12,1.566212692E12,1.566212692E12,1.566646344E12,1.566646344E12,1.567071334E12,1.567071334E12,1.567508507E12,1.567508507E12,1.567935649E12, 1.567935649E12, 1.568374212E12, 1.568374212E12]"," [1.558863212E12,1.55929586E12,1.559728791E12,1.5601599E12,1.560589582E12,1.561026607E12,1.561454836E12,1.561888229E12,1.562320207E12,1.562751856E12,1.563181463E12,1.563618614E12,1.564047988E12,1.564480274E12,1.564912267E12,1.565343999E12, 1.565773615E12]"," [1.568719125E12,1.569150328E12,1.569583549E12,1.570013418E12,1.570451296E12,1.570878011E12,1.571311086E12,1.571742422E12,1.572172948E12,1.572604827E12,1.573038304E12,1.573470317E12,1.573899161E12,1.574334225E12,1.575196752E12,1.57562775E12, 1.5760617E12]"," [1.545124633E12,1.545561224E12,1.545990899E12,1.546423114E12,1.546853425E12,1.547287051E12,1.547718865E12,1.548155787E12,1.548586075E12,1.549015326E12,1.549447719E12] QUOT;)), row.names = c(NA, -10L), class = "data.frame")

推荐答案

我们可以使用 str_extract_all 提取不是在 [ 之后的 ] 的字符,它返回一个 list,然后 unnest 列,并在 , 后跟一个或多个空格处拆分列值(\\s+) 与 separate_rows

We could extract the characters that are not a ] that succeeds the [ with str_extract_all, which returns a list, then unnest the columns, and split the column values at the , followed by one or more spaces (\\s+) with separate_rows

library(dplyr)
library(stringr)
library(tidyr)
df  %>% 
   mutate(across(everything(), str_extract_all, "(?<=\\[)[^]]+")) %>% 
   unnest(c(NDVIs, dates)) %>% 
   separate_rows(c(NDVIs, dates), sep=",\\s+", convert = TRUE)

-输出

# A tibble: 198 x 2
#      NDVIs         dates
#      <dbl>         <dbl>
# 1  0.00485 1527502095000
# 2  0.0319  1544955875000
# 3  0.0347  1544955875000
# 4  0.0569  1545126378000
# 5  0.0596  1545126378000
# 6  0.0500  1545394367000
# 7  0.0523  1545394367000
# 8  0.0525  1545561339000
# 9  0.0552  1545561339000
#10 -0.0102  1545820928000
# … with 188 more rows


或者使用 [] 不是在每个元素中,然后使用 str_remove_all 删除已经存在的元素并使用 separate_rows


Or with the [] is not in every element, then remove the already existing one with str_remove_all and use separate_rows

df %>%
   mutate(across(everything(), str_remove_all, "\\[|\\]")) %>% 
   separate_rows(c(NDVIs, dates), sep=",\\s+", convert = TRUE)
# A tibble: 198 x 2
#      NDVIs         dates
#      <dbl>         <dbl>
# 1  0.00485 1527502095000
# 2  0.0319  1544955875000
# 3  0.0347  1544955875000
# 4  0.0569  1545126378000
# 5  0.0596  1545126378000
# 6  0.0500  1545394367000
# 7  0.0523  1545394367000
# 8  0.0525  1545561339000
# 9  0.0552  1545561339000
#10 -0.0102  1545820928000
# … with 188 more rows

这篇关于将一列数组转换为 R 中的单个值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆