用R中的部分文件名替换主题ID
Replacing subject ID with part of filename in R
我有以下数据集(这是其中的一个子集):
list(structure(list(V1 = c(5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), V5 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("5480.jpg", "8186.jpg", "5621.jpg", "9471.jpg", "9007.jpg",
"6555.jpg", "7280.jpg", "4622.jpg", "5594.jpg", "2688.jpg", "9330.jpg",
"6834.jpg", "7705.jpg", "7080.jpg", "7233.jpg", "2700.jpg", "9430.jpg",
"9342.jpg", "8161.jpg", "8501.jpg", "2224.jpg", "7380.jpg", "9592.jpg",
"9341.jpg", "8193.jpg", "4180.jpg", "1510.jpg", "9611.jpg", "6831.jpg",
"2710.jpg", "2393.jpg", "2840.jpg", "7179.jpg", "7580.jpg", "5460.jpg",
"1721.jpg", "9400.jpg", "3400.jpg", "6830.jpg", "1650.jpg", "7480.jpg",
"8170.jpg"), V11 = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 4L, 3L, 1L, 0L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L
), V12 = c(1987.49322676, 2003.78750029, 2016.18643522, 2029.98508547,
2046.082619, 2059.69678371, 2072.27662962, 2088.35319136, 2103.33510062,
2114.53590582, 2130.63408597, 2143.73122899, 2156.81204578, 2172.88432076,
2186.75108144, 2199.0348313, 2215.13425956, 2227.83303149, 2241.3301239,
2257.42777441, 2271.80813707, 2283.5826768, 2299.68353984, 2312.48300952,
2325.8857002, 2341.97929395, 2355.55967609, 2368.15728052, 2384.22079626,
2398.39871977, 2410.39828415, 2426.49730549, 2441.29442761, 2452.69164213,
2468.77255819, 2483.74729774, 2494.91740773, 2511.01324173, 2524.71353057,
2537.21073726, 2553.29676658, 2565.76299181), V13 = c(1992.50788501,
2005.10323553, 2018.90335157, 2035.00063673, 2048.59856263, 2061.21330913,
2077.2857765, 2092.26887412, 2103.45229051, 2119.55267144, 2132.6498404,
2145.74762995, 2161.82758365, 2175.69277904, 2187.96811315, 2204.05114602,
2216.75108264, 2230.24948894, 2246.34679858, 2260.74340724, 2272.51665986,
2288.60037167, 2301.39998537, 2314.79927424, 2330.89755634, 2344.49464516,
2357.09652349, 2373.16490066, 2387.3319872, 2399.31499871, 2415.4143731,
2430.21259702, 2441.6115735, 2457.70809613, 2472.68103695, 2483.85340637,
2499.93071796, 2513.63010047, 2526.12895945, 2542.22682022, 2554.70658186,
2568.37009562), V14 = c(1993.75330082, 2006.15330628, 2019.95320617,
2036.0504665, 2049.66527255, 2062.25314096, 2078.33594597, 2093.30683345,
2104.50289596, 2120.60234076, 2133.69945801, 2146.7972698, 2162.86761203,
2176.7348525, 2189.01761019, 2205.10166161, 2217.80139939, 2231.29901038,
2247.39608311, 2261.79325268, 2273.55805641, 2289.6500982, 2302.4502089,
2315.84985489, 2331.94791451, 2345.5321581, 2358.14292019, 2374.20631859,
2388.37072667, 2400.36479062, 2416.46512504, 2431.26296093, 2442.66090881,
2458.75766914, 2473.73112172, 2484.89661205, 2500.98045728, 2514.68023375,
2527.17905154, 2543.27528682, 2555.74530719, 2569.41290119),
V15 = c(1999.77091881, 2012.17069799, 2025.96958909, 2042.06618575,
2055.68091664, 2068.26119022, 2084.34226467, 2099.31945827,
2110.51916708, 2126.61785271, 2139.71493787, 2152.80164663,
2168.87694609, 2182.74020292, 2195.02198667, 2211.11792771,
2223.81686668, 2237.31428137, 2253.41109216, 2267.80013181,
2279.56734828, 2295.66662992, 2308.46672665, 2321.86612209,
2337.96326175, 2351.54361379, 2364.1484122, 2380.21542344,
2394.38206849, 2406.38124464, 2422.48059629, 2437.27845236,
2448.6762584, 2464.76403815, 2479.73784862, 2490.90240349,
2506.99697921, 2520.6965112, 2533.19453821, 2549.27967288,
2561.75640359, 2575.41968263), V16 = c(1.97612254, 1.22228754,
1.07244078, 0.97520718, 1.01181801, 2.26757398, 1.69456861,
1.50956233, 0.94663342, 1.92908233, 1.00595454, 1.0762747,
1.43612961, 1.27413358, 1.12314117, 1.99375197, 0.76091358,
1.3039465, 0.90283654, 2.19145332, 0.79318283, 1.19297502,
0.69679878, 2.89059776, 1.02289159, 2.2764881, 1.6335375,
1.66840243, 1.11664701, 0.73965987, 1.32531222, 9, 0.94261183,
3.38459728, 2.31628939, 1.40065909, 1.38189958, 2.51368216,
3.20556865, 1.36864499, 0.77406087, 1.38290615), V17 = c(5,
1.3, 2.7, 5, 2.5, 1.5, 5, 3.9, 0.1, 5, 2, 2, 5, 2.8, 1.2,
5, 1.6, 2.4, 5, 3.3, 0.7, 5, 1.7, 2.3, 5, 2.5, 1.5, 5, 3.1,
0.9, 5, 3.7, 0.3, 5, 3.9, 0.1, 5, 2.6, 1.4, 5, 1.4, 2.6)), class = "data.frame", row.names = c(NA,
-42L)), structure(list(V1 = c(5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), V5 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("4680.jpg", "8341.jpg", "5820.jpg", "9620.jpg", "9042.jpg",
"6250.jpg", "7260.jpg", "4607.jpg", "5000.jpg", "2053.jpg", "9320.jpg",
"6821.jpg", "7950.jpg", "7035.jpg", "7090.jpg", "2490.jpg", "9530.jpg",
"9432.jpg", "8180.jpg", "8496.jpg", "2310.jpg", "9001.jpg", "9621.jpg",
"9331.jpg", "8370.jpg", "2345.jpg", "1463.jpg", "9830.jpg", "6550.jpg",
"4621.jpg", "2214.jpg", "7020.jpg", "7050.jpg", "8090.jpg", "4640.jpg",
"2208.jpg", "9495.jpg", "6243.jpg", "6510.jpg", "1722.jpg", "7430.jpg",
"8210.jpg"), V11 = c(2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 3L,
3L, 2L, 1L, 1L, 1L, 3L, 2L, 4L, 3L, 2L, 1L, 2L, 3L, 2L, 1L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L
), V12 = c(2830.68310042, 2846.98065809, 2859.37971014, 2874.17664546,
2890.27404476, 2903.86361556, 2917.43595781, 2933.51306339, 2948.51269093,
2960.7119709, 2976.80791512, 2989.90620453, 3003.978659, 3020.04446973,
3033.92808172, 3047.22799153, 3063.32629306, 3076.0237729, 3090.50472677,
3106.57706668, 3120.94306028, 3133.72735545, 3149.82645618, 3162.62568556,
3177.02317877, 3193.10291246, 3206.70018629, 3220.2760208, 3236.35869546,
3250.55899136, 3263.55684183, 3279.65437676, 3294.43550569, 3306.81603279,
3322.89098268, 3337.89114651, 3350.090125, 3366.18892521, 3379.88625864,
3393.36636756, 3409.45657284, 3421.92863112), V13 = c(2835.69763893,
2848.29687672, 2863.09567871, 2879.19290698, 2892.79072679, 2906.37089299,
2922.44575069, 2937.4291307, 2949.62896786, 2965.72728909, 2978.8255204,
2992.92205676, 3008.98648165, 3022.86163324, 3036.14441549, 3052.24415537,
3064.94277449, 3079.44004982, 3095.51054223, 3109.88410692, 3122.66037352,
3138.74359936, 3151.54372264, 3165.94112343, 3182.03895063, 3195.60906279,
3209.20861148, 3225.29223025, 3239.47566646, 3252.47514977, 3268.5729521,
3283.37067313, 3295.75078474, 3311.82475409, 3326.80706441, 3339.0077946,
3355.10713238, 3368.80466196, 3382.30225456, 3398.38243168, 3410.86352153,
3425.53895182), V14 = c(2836.94777244, 2849.34692278, 2864.14559232,
2880.24285764, 2893.84690476, 2907.41310083, 2923.48541492, 2938.47947031,
2950.67996483, 2966.77708896, 2979.87464765, 2993.96235442, 3010.02878474,
3023.90025773, 3037.19497426, 3053.294102, 3065.99249573, 3080.48936393,
3096.55416056, 3110.92728376, 3123.69894418, 3139.79323721, 3152.59289875,
3166.99070677, 3183.08791861, 3196.65164933, 3210.2491143, 3226.33053225,
3240.525598, 3253.52515929, 3269.62254874, 3284.42027478, 3296.79053404,
3312.87457863, 3327.85804343, 3340.05716338, 3356.1565393, 3369.85474886,
3383.33959784, 3399.43583915, 3411.90666723, 3426.57683964),
V15 = c(2842.96401751, 2855.36415856, 2870.16104373, 2886.25808632,
2899.8555051, 2913.42098007, 2929.49642383, 2944.49579289,
2956.69520627, 2972.7931764, 2985.88998124, 2999.97137667,
3016.03538767, 3029.91100089, 3043.21125712, 3059.30987528,
3072.00781376, 3086.49361198, 3102.56072439, 3116.93394175,
3129.71012547, 3145.80991423, 3158.6092781, 3173.00704328,
3189.0933848, 3202.68998311, 3216.25919269, 3232.34262987,
3246.54154305, 3259.54142981, 3275.63775768, 3290.42459499,
3302.7999622, 3318.8800581, 3333.87431569, 3346.07443061,
3362.1727909, 3375.87039741, 3389.35076625, 3405.4480156,
3417.91193105, 3432.58903452), V16 = c(2.27239183, 1.46103891,
1.11921971, 1.29976657, 1.49725865, 0.99838504, 1.80190757,
1.2957642, 1.8833041, 1.18930539, 1.91855918, 2.26358663,
1.44166371, 0.81771426, 1.87301313, 1.17165941, 1.40796688,
1.00468407, 1.47894186, 0.65129485, 1.00474347, 1.42280495,
0.94366453, 1.13311729, 1.08187487, 1.23435578, 1.01662371,
1.35480372, 1.05198223, 2.20586125, 0.6957003, 0.78074666,
1.77468312, 1.82572998, 1.82731178, 1.19673552, 1.50809555,
1.3260996, 1.57415788, 2.13057779, 1.61226701, 1.33258129
), V17 = c(5, 1.3, 3.7, 5, 2.5, 2.5, 5, 3.9, 1.1, 5, 2, 3,
5, 2.8, 2.2, 5, 1.6, 3.4, 5, 3.3, 1.7, 5, 1.7, 3.3, 5, 2.5,
2.5, 5, 3.1, 1.9, 5, 3.7, 1.3, 5, 3.9, 1.1, 5, 2.6, 2.4,
5, 1.4, 3.6)), class = "data.frame", row.names = c(NA, -42L
)), structure(list(V1 = c(5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), V5 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("4659.jpg", "8185.jpg", "5450.jpg", "9500.jpg", "6370.jpg",
"6210.jpg", "5660.jpg", "4623.jpg", "5260.jpg", "2692.jpg", "7361.jpg",
"6311.jpg", "7175.jpg", "2890.jpg", "7004.jpg", "2715.jpg", "9470.jpg",
"9415.jpg", "7400.jpg", "8400.jpg", "2346.jpg", "6315.jpg", "9520.jpg",
"9265.jpg", "8350.jpg", "4614.jpg", "1620.jpg", "9600.jpg", "6260.jpg",
"2753.jpg", "2381.jpg", "2493.jpg", "6150.jpg", "7270.jpg", "4652.jpg",
"2341.jpg", "9452.jpg", "3300.jpg", "6244.jpg", "2331.jpg", "5811.jpg",
"8080.jpg"), V11 = c(4L, 3L, 3L, 3L, 1L, 2L, 4L, 3L, 3L, 1L,
2L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 2L, 1L, 2L,
2L, 3L, 3L, 2L, 3L, 1L, 2L, 2L, 4L, 2L, 3L, 2L, 1L, 2L, 3L, 3L
), V12 = c(1778.39814779, 1794.66955017, 1807.05304984, 1820.85258368,
1836.95167276, 1850.56601201, 1863.1644145, 1879.24465175, 1894.21750028,
1905.38431034, 1921.48437907, 1934.58453319, 1947.68174495, 1963.77891255,
1977.65003662, 1989.92421098, 2005.99994814, 2018.68249564, 2032.18262005,
2048.28013671, 2062.67831834, 2074.47566186, 2090.53996975, 2103.31568701,
2116.71444193, 2132.8307998, 2146.43692569, 2159.0270518, 2175.11474787,
2189.27217003, 2201.27996151, 2217.34932513, 2232.14614334, 2243.54524832,
2259.64186742, 2274.61238405, 2285.78740174, 2301.85075916, 2315.52905518,
2328.01429283, 2344.10999375, 2356.60846788), V13 = c(1783.41158352,
1795.98600743, 1809.76883376, 1825.86920575, 1839.46897359, 1852.08223555,
1868.17934805, 1883.15087696, 1894.32458202, 1910.40056956, 1923.50095396,
1936.60073834, 1952.6980521, 1966.5953863, 1978.85978513, 1994.93266113,
2007.61650661, 2021.09947094, 2037.19887631, 2051.59635269, 2063.39423195,
2079.48183718, 2092.24855148, 2105.63167585, 2121.73130758, 2135.34794412,
2147.9454085, 2164.04274244, 2178.22329608, 2190.18059593, 2206.29698602,
2221.06337408, 2232.46304901, 2248.56140823, 2263.55890974, 2274.72298869,
2290.79597905, 2304.46236276, 2316.94521006, 2333.02830625, 2345.52714998,
2359.23146548), V14 = c(1784.65284278, 1797.03647351, 1810.81873264,
1826.91933961, 1840.53464514, 1853.13297127, 1869.22961427, 1884.1941818,
1895.36766193, 1911.45056078, 1924.55096298, 1937.65044915, 1953.74818115,
1967.6336646, 1979.89975464, 1995.97256449, 2008.65396355, 2022.1499575,
2038.24986552, 2052.64690288, 2064.44493158, 2080.52499275, 2093.28930834,
2106.68226018, 2122.78167243, 2136.39762533, 2148.99542426, 2165.0926221,
2179.25732171, 2191.25021477, 2207.33456886, 2222.11312128, 2233.51318712,
2249.61074977, 2264.59571618, 2275.77297399, 2291.83606674, 2305.50074661,
2317.9812663, 2334.07857459, 2346.57703731, 2360.27479414), V15 = c(1790.66065182,
1803.04183667, 1816.83597302, 1832.93579409, 1846.55080328, 1859.14829024,
1875.235072, 1890.20088982, 1901.37397943, 1917.46795723, 1930.56708003,
1943.6657331, 1959.76355729, 1973.64344119, 1985.90769956, 2001.98365794,
2014.66629685, 2028.16601121, 2044.26453256, 2058.66229876, 2070.46107834,
2086.53145683, 2099.29901093, 2112.69831484, 2128.81096062, 2142.4138868,
2155.01166613, 2171.09680187, 2185.26431756, 2197.26342651, 2213.34656808,
2228.13022975, 2239.52926994, 2255.62701449, 2270.60771614, 2281.7796938,
2297.84619268, 2311.51158522, 2323.99521848, 2340.09452581, 2352.59196262,
2366.28997997), V16 = c(4.00061554, 1.8774984, 1.943505, 3.39527626,
2.00290151, 2.26146808, 1.168004, 3.17210792, 3.21362597, 4.00228237,
2.89042446, 3.90804253, 3.80087994, 3.0794306, 2.0969269, 4.00193167,
3.78258696, 3.40912195, 3.91980072, 3.9490749, 3.89213988, 4.00200561,
3.99941022, 3.96590426, 2.49138412, 3.99906077, 3.99919385, 2.28047369,
3.76505293, 3.90544913, 3.9988001, 3.05481067, 3.95526409, 3.89273008,
3.99936508, 3.70160298, 3.88020859, 3.23017589, 2.63215948, 3.99974673,
4.00203896, 2.95468224), V17 = c(5, 1.3, 2.7, 5, 2.5, 1.5, 5,
3.9, 0.1, 5, 2, 2, 5, 2.8, 1.2, 5, 1.6, 2.4, 5, 3.3, 0.7, 5,
1.7, 2.3, 5, 2.5, 1.5, 5, 3.1, 0.9, 5, 3.7, 0.3, 5, 3.9, 0.1,
5, 2.6, 1.4, 5, 1.4, 2.6)), class = "data.frame", row.names = c(NA,
-42L)))
每个数据列表的文件名是:
filelist[1:3]
[1] "C:/Users/Desktop/ER_raw_data/output_subject5001_prepost1_run1.txt"
[2] "C:/Users/Desktop/ER_raw_data/output_subject5002_prepost1_run1.txt"
[3] "C:/Users/Desktop/ER_raw_data/output_subject5004_prepost1_run1.txt"
我可以使用以下命令从文件名中提取主题 ID:
IDs <- unique(na.omit(as.numeric(unlist(strsplit(unlist(filelist), "[^0-9]+")))))[2]
与第三个文件名一样,一些文件名的主题 ID 与相应列表第一列中的数据不同。我现在正在努力用对应于该列表的 文件名 ID 替换列表 column1 ID。
我知道我可以使用 replace
函数,但我不确定如何避免 100 多个 ID 提取和替换的 for 循环。帮助表示赞赏。
我认为您可以使用以下解决方案:
IDs <- gsub(".*subject(\d+).*", "\1", filename, perl = TRUE)
Map(function(x, y) {
x[["V1"]] <- y
x
}, df, IDs)
- 我们可以使用
readr
中的 parse_number
来获取 ID
- 为列表中的每个 df 创建一个
tibble
- 使用
purrr
包中的 map2
迭代每个小标题和 mutate
V1
library(tidyverse)
new_filelist <- as.list(parse_number(filelist))
my_list <- map(my_list, as_tibble)
map2(my_list, new_filelist, ~ mutate(., V1 =.y))
[[1]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5001 1 1 1 0 1 1 D P 5480.j~ 1 1987. 1993. 1994. 2000. 1.98 5
2 5001 1 1 1 0 1 2 D P 8186.j~ 1 2004. 2005. 2006. 2012. 1.22 1.3
3 5001 1 1 1 0 1 3 D P 5621.j~ 1 2016. 2019. 2020. 2026. 1.07 2.7
4 5001 1 1 1 0 2 4 V N 9471.j~ 1 2030. 2035. 2036. 2042. 0.975 5
5 5001 1 1 1 0 2 5 V N 9007.j~ 1 2046. 2049. 2050. 2056. 1.01 2.5
6 5001 1 1 1 0 2 6 V N 6555.j~ 1 2060. 2061. 2062. 2068. 2.27 1.5
7 5001 1 1 1 0 3 7 I P 7280.j~ 2 2072. 2077. 2078. 2084. 1.69 5
8 5001 1 1 1 0 3 8 I P 4622.j~ 3 2088. 2092. 2093. 2099. 1.51 3.9
9 5001 1 1 1 0 3 9 I P 5594.j~ 3 2103. 2103. 2105. 2111. 0.947 0.1
10 5001 1 1 1 0 4 10 D N 2688.j~ 2 2115. 2120. 2121. 2127. 1.93 5
# ... with 32 more rows
[[2]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5002 1 1 0 1 1 1 D P 4680.j~ 2 2831. 2836. 2837. 2843. 2.27 5
2 5002 1 1 0 1 1 2 D P 8341.j~ 2 2847. 2848. 2849. 2855. 1.46 1.3
3 5002 1 1 0 1 1 3 D P 5820.j~ 3 2859. 2863. 2864. 2870. 1.12 3.7
4 5002 1 1 0 1 2 4 V N 9620.j~ 2 2874. 2879. 2880. 2886. 1.30 5
5 5002 1 1 0 1 2 5 V N 9042.j~ 3 2890. 2893. 2894. 2900. 1.50 2.5
6 5002 1 1 0 1 2 6 V N 6250.j~ 2 2904. 2906. 2907. 2913. 0.998 2.5
7 5002 1 1 0 1 3 7 I P 7260.j~ 2 2917. 2922. 2923. 2929. 1.80 5
8 5002 1 1 0 1 3 8 I P 4607.j~ 3 2934. 2937. 2938. 2944. 1.30 3.9
9 5002 1 1 0 1 3 9 I P 5000.j~ 2 2949. 2950. 2951. 2957. 1.88 1.1
10 5002 1 1 0 1 4 10 D N 2053.j~ 3 2961. 2966. 2967. 2973. 1.19 5
# ... with 32 more rows
[[3]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5004 1 1 1 2 1 1 D P 4659.j~ 4 1778. 1783. 1785. 1791. 4.00 5
2 5004 1 1 1 2 1 2 D P 8185.j~ 3 1795. 1796. 1797. 1803. 1.88 1.3
3 5004 1 1 1 2 1 3 D P 5450.j~ 3 1807. 1810. 1811. 1817. 1.94 2.7
4 5004 1 1 1 2 2 4 V N 9500.j~ 3 1821. 1826. 1827. 1833. 3.40 5
5 5004 1 1 1 2 2 5 V N 6370.j~ 1 1837. 1839. 1841. 1847. 2.00 2.5
6 5004 1 1 1 2 2 6 V N 6210.j~ 2 1851. 1852. 1853. 1859. 2.26 1.5
7 5004 1 1 1 2 3 7 I P 5660.j~ 4 1863. 1868. 1869. 1875. 1.17 5
8 5004 1 1 1 2 3 8 I P 4623.j~ 3 1879. 1883. 1884. 1890. 3.17 3.9
9 5004 1 1 1 2 3 9 I P 5260.j~ 3 1894. 1894. 1895. 1901. 3.21 0.1
10 5004 1 1 1 2 4 10 D N 2692.j~ 1 1905. 1910. 1911. 1917. 4.00 5
# ... with 32 more rows
使用str_extract
library(stringr)
library(dplyr)
library(purrr)
ids <- str_extract(basename(filelist), '(?<=subject)\d+')
map2(list1, ids, ~ .x %>%
mutate(V1 = .y))
我有以下数据集(这是其中的一个子集):
list(structure(list(V1 = c(5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L, 5001L,
5001L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), V5 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("5480.jpg", "8186.jpg", "5621.jpg", "9471.jpg", "9007.jpg",
"6555.jpg", "7280.jpg", "4622.jpg", "5594.jpg", "2688.jpg", "9330.jpg",
"6834.jpg", "7705.jpg", "7080.jpg", "7233.jpg", "2700.jpg", "9430.jpg",
"9342.jpg", "8161.jpg", "8501.jpg", "2224.jpg", "7380.jpg", "9592.jpg",
"9341.jpg", "8193.jpg", "4180.jpg", "1510.jpg", "9611.jpg", "6831.jpg",
"2710.jpg", "2393.jpg", "2840.jpg", "7179.jpg", "7580.jpg", "5460.jpg",
"1721.jpg", "9400.jpg", "3400.jpg", "6830.jpg", "1650.jpg", "7480.jpg",
"8170.jpg"), V11 = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 4L, 3L, 1L, 0L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L
), V12 = c(1987.49322676, 2003.78750029, 2016.18643522, 2029.98508547,
2046.082619, 2059.69678371, 2072.27662962, 2088.35319136, 2103.33510062,
2114.53590582, 2130.63408597, 2143.73122899, 2156.81204578, 2172.88432076,
2186.75108144, 2199.0348313, 2215.13425956, 2227.83303149, 2241.3301239,
2257.42777441, 2271.80813707, 2283.5826768, 2299.68353984, 2312.48300952,
2325.8857002, 2341.97929395, 2355.55967609, 2368.15728052, 2384.22079626,
2398.39871977, 2410.39828415, 2426.49730549, 2441.29442761, 2452.69164213,
2468.77255819, 2483.74729774, 2494.91740773, 2511.01324173, 2524.71353057,
2537.21073726, 2553.29676658, 2565.76299181), V13 = c(1992.50788501,
2005.10323553, 2018.90335157, 2035.00063673, 2048.59856263, 2061.21330913,
2077.2857765, 2092.26887412, 2103.45229051, 2119.55267144, 2132.6498404,
2145.74762995, 2161.82758365, 2175.69277904, 2187.96811315, 2204.05114602,
2216.75108264, 2230.24948894, 2246.34679858, 2260.74340724, 2272.51665986,
2288.60037167, 2301.39998537, 2314.79927424, 2330.89755634, 2344.49464516,
2357.09652349, 2373.16490066, 2387.3319872, 2399.31499871, 2415.4143731,
2430.21259702, 2441.6115735, 2457.70809613, 2472.68103695, 2483.85340637,
2499.93071796, 2513.63010047, 2526.12895945, 2542.22682022, 2554.70658186,
2568.37009562), V14 = c(1993.75330082, 2006.15330628, 2019.95320617,
2036.0504665, 2049.66527255, 2062.25314096, 2078.33594597, 2093.30683345,
2104.50289596, 2120.60234076, 2133.69945801, 2146.7972698, 2162.86761203,
2176.7348525, 2189.01761019, 2205.10166161, 2217.80139939, 2231.29901038,
2247.39608311, 2261.79325268, 2273.55805641, 2289.6500982, 2302.4502089,
2315.84985489, 2331.94791451, 2345.5321581, 2358.14292019, 2374.20631859,
2388.37072667, 2400.36479062, 2416.46512504, 2431.26296093, 2442.66090881,
2458.75766914, 2473.73112172, 2484.89661205, 2500.98045728, 2514.68023375,
2527.17905154, 2543.27528682, 2555.74530719, 2569.41290119),
V15 = c(1999.77091881, 2012.17069799, 2025.96958909, 2042.06618575,
2055.68091664, 2068.26119022, 2084.34226467, 2099.31945827,
2110.51916708, 2126.61785271, 2139.71493787, 2152.80164663,
2168.87694609, 2182.74020292, 2195.02198667, 2211.11792771,
2223.81686668, 2237.31428137, 2253.41109216, 2267.80013181,
2279.56734828, 2295.66662992, 2308.46672665, 2321.86612209,
2337.96326175, 2351.54361379, 2364.1484122, 2380.21542344,
2394.38206849, 2406.38124464, 2422.48059629, 2437.27845236,
2448.6762584, 2464.76403815, 2479.73784862, 2490.90240349,
2506.99697921, 2520.6965112, 2533.19453821, 2549.27967288,
2561.75640359, 2575.41968263), V16 = c(1.97612254, 1.22228754,
1.07244078, 0.97520718, 1.01181801, 2.26757398, 1.69456861,
1.50956233, 0.94663342, 1.92908233, 1.00595454, 1.0762747,
1.43612961, 1.27413358, 1.12314117, 1.99375197, 0.76091358,
1.3039465, 0.90283654, 2.19145332, 0.79318283, 1.19297502,
0.69679878, 2.89059776, 1.02289159, 2.2764881, 1.6335375,
1.66840243, 1.11664701, 0.73965987, 1.32531222, 9, 0.94261183,
3.38459728, 2.31628939, 1.40065909, 1.38189958, 2.51368216,
3.20556865, 1.36864499, 0.77406087, 1.38290615), V17 = c(5,
1.3, 2.7, 5, 2.5, 1.5, 5, 3.9, 0.1, 5, 2, 2, 5, 2.8, 1.2,
5, 1.6, 2.4, 5, 3.3, 0.7, 5, 1.7, 2.3, 5, 2.5, 1.5, 5, 3.1,
0.9, 5, 3.7, 0.3, 5, 3.9, 0.1, 5, 2.6, 1.4, 5, 1.4, 2.6)), class = "data.frame", row.names = c(NA,
-42L)), structure(list(V1 = c(5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L, 5002L,
5002L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), V5 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("4680.jpg", "8341.jpg", "5820.jpg", "9620.jpg", "9042.jpg",
"6250.jpg", "7260.jpg", "4607.jpg", "5000.jpg", "2053.jpg", "9320.jpg",
"6821.jpg", "7950.jpg", "7035.jpg", "7090.jpg", "2490.jpg", "9530.jpg",
"9432.jpg", "8180.jpg", "8496.jpg", "2310.jpg", "9001.jpg", "9621.jpg",
"9331.jpg", "8370.jpg", "2345.jpg", "1463.jpg", "9830.jpg", "6550.jpg",
"4621.jpg", "2214.jpg", "7020.jpg", "7050.jpg", "8090.jpg", "4640.jpg",
"2208.jpg", "9495.jpg", "6243.jpg", "6510.jpg", "1722.jpg", "7430.jpg",
"8210.jpg"), V11 = c(2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 3L,
3L, 2L, 1L, 1L, 1L, 3L, 2L, 4L, 3L, 2L, 1L, 2L, 3L, 2L, 1L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L
), V12 = c(2830.68310042, 2846.98065809, 2859.37971014, 2874.17664546,
2890.27404476, 2903.86361556, 2917.43595781, 2933.51306339, 2948.51269093,
2960.7119709, 2976.80791512, 2989.90620453, 3003.978659, 3020.04446973,
3033.92808172, 3047.22799153, 3063.32629306, 3076.0237729, 3090.50472677,
3106.57706668, 3120.94306028, 3133.72735545, 3149.82645618, 3162.62568556,
3177.02317877, 3193.10291246, 3206.70018629, 3220.2760208, 3236.35869546,
3250.55899136, 3263.55684183, 3279.65437676, 3294.43550569, 3306.81603279,
3322.89098268, 3337.89114651, 3350.090125, 3366.18892521, 3379.88625864,
3393.36636756, 3409.45657284, 3421.92863112), V13 = c(2835.69763893,
2848.29687672, 2863.09567871, 2879.19290698, 2892.79072679, 2906.37089299,
2922.44575069, 2937.4291307, 2949.62896786, 2965.72728909, 2978.8255204,
2992.92205676, 3008.98648165, 3022.86163324, 3036.14441549, 3052.24415537,
3064.94277449, 3079.44004982, 3095.51054223, 3109.88410692, 3122.66037352,
3138.74359936, 3151.54372264, 3165.94112343, 3182.03895063, 3195.60906279,
3209.20861148, 3225.29223025, 3239.47566646, 3252.47514977, 3268.5729521,
3283.37067313, 3295.75078474, 3311.82475409, 3326.80706441, 3339.0077946,
3355.10713238, 3368.80466196, 3382.30225456, 3398.38243168, 3410.86352153,
3425.53895182), V14 = c(2836.94777244, 2849.34692278, 2864.14559232,
2880.24285764, 2893.84690476, 2907.41310083, 2923.48541492, 2938.47947031,
2950.67996483, 2966.77708896, 2979.87464765, 2993.96235442, 3010.02878474,
3023.90025773, 3037.19497426, 3053.294102, 3065.99249573, 3080.48936393,
3096.55416056, 3110.92728376, 3123.69894418, 3139.79323721, 3152.59289875,
3166.99070677, 3183.08791861, 3196.65164933, 3210.2491143, 3226.33053225,
3240.525598, 3253.52515929, 3269.62254874, 3284.42027478, 3296.79053404,
3312.87457863, 3327.85804343, 3340.05716338, 3356.1565393, 3369.85474886,
3383.33959784, 3399.43583915, 3411.90666723, 3426.57683964),
V15 = c(2842.96401751, 2855.36415856, 2870.16104373, 2886.25808632,
2899.8555051, 2913.42098007, 2929.49642383, 2944.49579289,
2956.69520627, 2972.7931764, 2985.88998124, 2999.97137667,
3016.03538767, 3029.91100089, 3043.21125712, 3059.30987528,
3072.00781376, 3086.49361198, 3102.56072439, 3116.93394175,
3129.71012547, 3145.80991423, 3158.6092781, 3173.00704328,
3189.0933848, 3202.68998311, 3216.25919269, 3232.34262987,
3246.54154305, 3259.54142981, 3275.63775768, 3290.42459499,
3302.7999622, 3318.8800581, 3333.87431569, 3346.07443061,
3362.1727909, 3375.87039741, 3389.35076625, 3405.4480156,
3417.91193105, 3432.58903452), V16 = c(2.27239183, 1.46103891,
1.11921971, 1.29976657, 1.49725865, 0.99838504, 1.80190757,
1.2957642, 1.8833041, 1.18930539, 1.91855918, 2.26358663,
1.44166371, 0.81771426, 1.87301313, 1.17165941, 1.40796688,
1.00468407, 1.47894186, 0.65129485, 1.00474347, 1.42280495,
0.94366453, 1.13311729, 1.08187487, 1.23435578, 1.01662371,
1.35480372, 1.05198223, 2.20586125, 0.6957003, 0.78074666,
1.77468312, 1.82572998, 1.82731178, 1.19673552, 1.50809555,
1.3260996, 1.57415788, 2.13057779, 1.61226701, 1.33258129
), V17 = c(5, 1.3, 3.7, 5, 2.5, 2.5, 5, 3.9, 1.1, 5, 2, 3,
5, 2.8, 2.2, 5, 1.6, 3.4, 5, 3.3, 1.7, 5, 1.7, 3.3, 5, 2.5,
2.5, 5, 3.1, 1.9, 5, 3.7, 1.3, 5, 3.9, 1.1, 5, 2.6, 2.4,
5, 1.4, 3.6)), class = "data.frame", row.names = c(NA, -42L
)), structure(list(V1 = c(5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L, 5003L,
5003L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), V5 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), V6 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L,
14L), V7 = 1:42, V8 = c("D", "D", "D", "V", "V", "V", "I", "I",
"I", "D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V",
"D", "D", "D", "V", "V", "V", "I", "I", "I", "V", "V", "V", "D",
"D", "D", "V", "V", "V", "I", "I", "I"), V9 = c("P", "P", "P",
"N", "N", "N", "P", "P", "P", "N", "N", "N", "T", "T", "T", "N",
"N", "N", "P", "P", "P", "N", "N", "N", "P", "P", "P", "N", "N",
"N", "T", "T", "T", "P", "P", "P", "N", "N", "N", "P", "P", "P"
), V10 = c("4659.jpg", "8185.jpg", "5450.jpg", "9500.jpg", "6370.jpg",
"6210.jpg", "5660.jpg", "4623.jpg", "5260.jpg", "2692.jpg", "7361.jpg",
"6311.jpg", "7175.jpg", "2890.jpg", "7004.jpg", "2715.jpg", "9470.jpg",
"9415.jpg", "7400.jpg", "8400.jpg", "2346.jpg", "6315.jpg", "9520.jpg",
"9265.jpg", "8350.jpg", "4614.jpg", "1620.jpg", "9600.jpg", "6260.jpg",
"2753.jpg", "2381.jpg", "2493.jpg", "6150.jpg", "7270.jpg", "4652.jpg",
"2341.jpg", "9452.jpg", "3300.jpg", "6244.jpg", "2331.jpg", "5811.jpg",
"8080.jpg"), V11 = c(4L, 3L, 3L, 3L, 1L, 2L, 4L, 3L, 3L, 1L,
2L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 2L, 1L, 2L,
2L, 3L, 3L, 2L, 3L, 1L, 2L, 2L, 4L, 2L, 3L, 2L, 1L, 2L, 3L, 3L
), V12 = c(1778.39814779, 1794.66955017, 1807.05304984, 1820.85258368,
1836.95167276, 1850.56601201, 1863.1644145, 1879.24465175, 1894.21750028,
1905.38431034, 1921.48437907, 1934.58453319, 1947.68174495, 1963.77891255,
1977.65003662, 1989.92421098, 2005.99994814, 2018.68249564, 2032.18262005,
2048.28013671, 2062.67831834, 2074.47566186, 2090.53996975, 2103.31568701,
2116.71444193, 2132.8307998, 2146.43692569, 2159.0270518, 2175.11474787,
2189.27217003, 2201.27996151, 2217.34932513, 2232.14614334, 2243.54524832,
2259.64186742, 2274.61238405, 2285.78740174, 2301.85075916, 2315.52905518,
2328.01429283, 2344.10999375, 2356.60846788), V13 = c(1783.41158352,
1795.98600743, 1809.76883376, 1825.86920575, 1839.46897359, 1852.08223555,
1868.17934805, 1883.15087696, 1894.32458202, 1910.40056956, 1923.50095396,
1936.60073834, 1952.6980521, 1966.5953863, 1978.85978513, 1994.93266113,
2007.61650661, 2021.09947094, 2037.19887631, 2051.59635269, 2063.39423195,
2079.48183718, 2092.24855148, 2105.63167585, 2121.73130758, 2135.34794412,
2147.9454085, 2164.04274244, 2178.22329608, 2190.18059593, 2206.29698602,
2221.06337408, 2232.46304901, 2248.56140823, 2263.55890974, 2274.72298869,
2290.79597905, 2304.46236276, 2316.94521006, 2333.02830625, 2345.52714998,
2359.23146548), V14 = c(1784.65284278, 1797.03647351, 1810.81873264,
1826.91933961, 1840.53464514, 1853.13297127, 1869.22961427, 1884.1941818,
1895.36766193, 1911.45056078, 1924.55096298, 1937.65044915, 1953.74818115,
1967.6336646, 1979.89975464, 1995.97256449, 2008.65396355, 2022.1499575,
2038.24986552, 2052.64690288, 2064.44493158, 2080.52499275, 2093.28930834,
2106.68226018, 2122.78167243, 2136.39762533, 2148.99542426, 2165.0926221,
2179.25732171, 2191.25021477, 2207.33456886, 2222.11312128, 2233.51318712,
2249.61074977, 2264.59571618, 2275.77297399, 2291.83606674, 2305.50074661,
2317.9812663, 2334.07857459, 2346.57703731, 2360.27479414), V15 = c(1790.66065182,
1803.04183667, 1816.83597302, 1832.93579409, 1846.55080328, 1859.14829024,
1875.235072, 1890.20088982, 1901.37397943, 1917.46795723, 1930.56708003,
1943.6657331, 1959.76355729, 1973.64344119, 1985.90769956, 2001.98365794,
2014.66629685, 2028.16601121, 2044.26453256, 2058.66229876, 2070.46107834,
2086.53145683, 2099.29901093, 2112.69831484, 2128.81096062, 2142.4138868,
2155.01166613, 2171.09680187, 2185.26431756, 2197.26342651, 2213.34656808,
2228.13022975, 2239.52926994, 2255.62701449, 2270.60771614, 2281.7796938,
2297.84619268, 2311.51158522, 2323.99521848, 2340.09452581, 2352.59196262,
2366.28997997), V16 = c(4.00061554, 1.8774984, 1.943505, 3.39527626,
2.00290151, 2.26146808, 1.168004, 3.17210792, 3.21362597, 4.00228237,
2.89042446, 3.90804253, 3.80087994, 3.0794306, 2.0969269, 4.00193167,
3.78258696, 3.40912195, 3.91980072, 3.9490749, 3.89213988, 4.00200561,
3.99941022, 3.96590426, 2.49138412, 3.99906077, 3.99919385, 2.28047369,
3.76505293, 3.90544913, 3.9988001, 3.05481067, 3.95526409, 3.89273008,
3.99936508, 3.70160298, 3.88020859, 3.23017589, 2.63215948, 3.99974673,
4.00203896, 2.95468224), V17 = c(5, 1.3, 2.7, 5, 2.5, 1.5, 5,
3.9, 0.1, 5, 2, 2, 5, 2.8, 1.2, 5, 1.6, 2.4, 5, 3.3, 0.7, 5,
1.7, 2.3, 5, 2.5, 1.5, 5, 3.1, 0.9, 5, 3.7, 0.3, 5, 3.9, 0.1,
5, 2.6, 1.4, 5, 1.4, 2.6)), class = "data.frame", row.names = c(NA,
-42L)))
每个数据列表的文件名是:
filelist[1:3]
[1] "C:/Users/Desktop/ER_raw_data/output_subject5001_prepost1_run1.txt"
[2] "C:/Users/Desktop/ER_raw_data/output_subject5002_prepost1_run1.txt"
[3] "C:/Users/Desktop/ER_raw_data/output_subject5004_prepost1_run1.txt"
我可以使用以下命令从文件名中提取主题 ID:
IDs <- unique(na.omit(as.numeric(unlist(strsplit(unlist(filelist), "[^0-9]+")))))[2]
与第三个文件名一样,一些文件名的主题 ID 与相应列表第一列中的数据不同。我现在正在努力用对应于该列表的 文件名 ID 替换列表 column1 ID。
我知道我可以使用 replace
函数,但我不确定如何避免 100 多个 ID 提取和替换的 for 循环。帮助表示赞赏。
我认为您可以使用以下解决方案:
IDs <- gsub(".*subject(\d+).*", "\1", filename, perl = TRUE)
Map(function(x, y) {
x[["V1"]] <- y
x
}, df, IDs)
- 我们可以使用
readr
中的parse_number
来获取 ID - 为列表中的每个 df 创建一个
tibble
- 使用
purrr
包中的map2
迭代每个小标题和mutate
V1
library(tidyverse)
new_filelist <- as.list(parse_number(filelist))
my_list <- map(my_list, as_tibble)
map2(my_list, new_filelist, ~ mutate(., V1 =.y))
[[1]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5001 1 1 1 0 1 1 D P 5480.j~ 1 1987. 1993. 1994. 2000. 1.98 5
2 5001 1 1 1 0 1 2 D P 8186.j~ 1 2004. 2005. 2006. 2012. 1.22 1.3
3 5001 1 1 1 0 1 3 D P 5621.j~ 1 2016. 2019. 2020. 2026. 1.07 2.7
4 5001 1 1 1 0 2 4 V N 9471.j~ 1 2030. 2035. 2036. 2042. 0.975 5
5 5001 1 1 1 0 2 5 V N 9007.j~ 1 2046. 2049. 2050. 2056. 1.01 2.5
6 5001 1 1 1 0 2 6 V N 6555.j~ 1 2060. 2061. 2062. 2068. 2.27 1.5
7 5001 1 1 1 0 3 7 I P 7280.j~ 2 2072. 2077. 2078. 2084. 1.69 5
8 5001 1 1 1 0 3 8 I P 4622.j~ 3 2088. 2092. 2093. 2099. 1.51 3.9
9 5001 1 1 1 0 3 9 I P 5594.j~ 3 2103. 2103. 2105. 2111. 0.947 0.1
10 5001 1 1 1 0 4 10 D N 2688.j~ 2 2115. 2120. 2121. 2127. 1.93 5
# ... with 32 more rows
[[2]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5002 1 1 0 1 1 1 D P 4680.j~ 2 2831. 2836. 2837. 2843. 2.27 5
2 5002 1 1 0 1 1 2 D P 8341.j~ 2 2847. 2848. 2849. 2855. 1.46 1.3
3 5002 1 1 0 1 1 3 D P 5820.j~ 3 2859. 2863. 2864. 2870. 1.12 3.7
4 5002 1 1 0 1 2 4 V N 9620.j~ 2 2874. 2879. 2880. 2886. 1.30 5
5 5002 1 1 0 1 2 5 V N 9042.j~ 3 2890. 2893. 2894. 2900. 1.50 2.5
6 5002 1 1 0 1 2 6 V N 6250.j~ 2 2904. 2906. 2907. 2913. 0.998 2.5
7 5002 1 1 0 1 3 7 I P 7260.j~ 2 2917. 2922. 2923. 2929. 1.80 5
8 5002 1 1 0 1 3 8 I P 4607.j~ 3 2934. 2937. 2938. 2944. 1.30 3.9
9 5002 1 1 0 1 3 9 I P 5000.j~ 2 2949. 2950. 2951. 2957. 1.88 1.1
10 5002 1 1 0 1 4 10 D N 2053.j~ 3 2961. 2966. 2967. 2973. 1.19 5
# ... with 32 more rows
[[3]]
# A tibble: 42 x 17
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
<dbl> <int> <int> <int> <int> <int> <int> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5004 1 1 1 2 1 1 D P 4659.j~ 4 1778. 1783. 1785. 1791. 4.00 5
2 5004 1 1 1 2 1 2 D P 8185.j~ 3 1795. 1796. 1797. 1803. 1.88 1.3
3 5004 1 1 1 2 1 3 D P 5450.j~ 3 1807. 1810. 1811. 1817. 1.94 2.7
4 5004 1 1 1 2 2 4 V N 9500.j~ 3 1821. 1826. 1827. 1833. 3.40 5
5 5004 1 1 1 2 2 5 V N 6370.j~ 1 1837. 1839. 1841. 1847. 2.00 2.5
6 5004 1 1 1 2 2 6 V N 6210.j~ 2 1851. 1852. 1853. 1859. 2.26 1.5
7 5004 1 1 1 2 3 7 I P 5660.j~ 4 1863. 1868. 1869. 1875. 1.17 5
8 5004 1 1 1 2 3 8 I P 4623.j~ 3 1879. 1883. 1884. 1890. 3.17 3.9
9 5004 1 1 1 2 3 9 I P 5260.j~ 3 1894. 1894. 1895. 1901. 3.21 0.1
10 5004 1 1 1 2 4 10 D N 2692.j~ 1 1905. 1910. 1911. 1917. 4.00 5
# ... with 32 more rows
使用str_extract
library(stringr)
library(dplyr)
library(purrr)
ids <- str_extract(basename(filelist), '(?<=subject)\d+')
map2(list1, ids, ~ .x %>%
mutate(V1 = .y))